

package bayreuth;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import javax.swing.JOptionPane;
import java.util.Date;

public class anchoring
{
   static final String[] Options={"1","2","3","12","13","23","123"}; //possible options for each level of the ranking

 //---------------------------------------------Distance Matrixes for KS, DP and CS--------------------------------

   static final int[][] DPDistanceMatrix= new int[][] {{0,1,1,1,1,1,1,2,2,2,2,2,2},{1,0,2,2,2,2,2,1,3,1,2,3,3},{1,2,0,2,2,2,2,3,1,3,3,1,3},
	   {1,2,2,0,2,2,2,3,3,3,1,3,1},{1,2,2,2,0,2,2,1,1,3,3,3,3},{1,2,2,2,2,0,2,3,3,1,1,3,3},
	   {1,2,2,2,2,2,0,3,3,3,3,1,1},{2,1,3,3,1,3,3,0,2,2,4,4,4},{2,3,1,3,1,3,3,2,0,4,4,2,4},
	   {2,1,3,3,3,1,3,2,4,0,2,4,4},{2,2,3,1,3,1,3,4,4,2,0,4,2},{2,3,1,3,3,3,1,4,2,4,4,0,2},
	   {2,3,3,1,3,3,1,4,4,4,2,2,0}};
	   
	    static final int[][] CSDistanceMatrix= new int[][] {{0,2,2,2,2,2,2,2,2,2,2,2,2},{2,0,3,3,2,2,4,1,3,1,3,4,4},
	  {2,3,0,3,2,4,2,3,1,4,4,1,3},{2,3,3,0,4,2,2,4,4,3,1,3,1},{2,2,2,4,0,3,3,1,1,3,4,3,4},{2,2,4,2,3,0,3,3,4,1,1,4,3},
	  {2,4,2,2,3,3,0,4,3,4,3,1,1},{2,1,3,4,1,3,4,0,2,2,4,4,4},{2,3,1,4,1,4,3,2,0,4,4,2,4},{2,1,4,3,3,1,4,2,4,0,2,4,4},
	  {2,3,4,1,4,1,3,4,4,2,0,4,2},{2,4,1,3,3,4,1,4,2,4,4,0,2},{2,4,3,1,4,3,1,4,4,4,2,2,0}};

	    static final int[][] KDistanceMatrix= new int[][] {{0,2,2,2,2,2,2,3,3,3,3,3,3},{2,0,4,4,2,2,4,1,3,1,3,5,5},
	    {2,4,0,4,2,4,2,3,1,5,5,1,3},{2,4,4,0,4,2,2,5,5,3,1,3,1},{2,2,2,4,0,4,4,1,1,3,5,3,5},{2,2,4,2,4,0,4,3,5,1,1,5,3},
	{2,4,2,2,4,4,0,5,3,5,3,1,1},{3,1,3,5,1,3,5,0,2,2,4,4,6},{3,3,1,5,1,5,3,2,0,4,6,2,4},{3,1,5,3,3,1,5,2,4,0,2,6,4},
	   {3,3,5,1,5,1,3,4,6,2,0,4,2},{3,5,1,3,3,5,1,4,2,6,4,0,2},{3,5,3,1,5,3,1,6,4,4,2,2,0}};

 //------------------------------------------------------------------------------------------------------------------------

   static List<String>[] Rankings;        //list of rankings
   static List<String>[] StrictRankings;        //list of strict rankings

   double[] initialRel;                   //reliabilities agents assign to themselves
   int[] initialVotes;
   double[][][] EachRoundReliabilities;   //to be used when reliabilities will change from each round to next
   Random r=new Random();                 //random generator
   public int numAgents;  //group size
   int numDelibRounds; //number of deliberation rounds
	double[][] AssignedReliabilities;
	static String fileDate = new SimpleDateFormat("ddMMHHmm").format(new Date());

//----------------------------------------------------initializing--------------------------------------------------------------------

public anchoring(int numAgents, int numDelibRounds, double Reliabilities[])
//public anchoring(int numAgents, int numDelibRounds)

{
this.Rankings=new List[13];
this.StrictRankings=new List[6];

this.numAgents=numAgents;
this.numDelibRounds=numDelibRounds;
this.EachRoundReliabilities=new double[numDelibRounds][numAgents][numAgents];
this.initialRel= new double[this.numAgents];
this.AssignedReliabilities= new double[this.numAgents][this.numAgents];

//----fixed reliabilities for all simulation
/*
for(int i=0; i< this.numAgents;i++)
{
//this.initialRel[i]=0.6;  //setting rel1 to be the reliability that agents assign to each other
this.initialRel[i]=(r.nextDouble()*(0.5)) + 0.5;  //setting reliabilities  randomly
}



for(int s=0; s<this.numAgents; s++ )
{
	for(int w=0; w < this.numAgents; w++)
	{
//	if(s==w) {
//			this.AssignedReliabilities[s][w]= initialRel[w];
//		}else {
		//this.AssignedReliabilities[s][w]= 1-initialRel[w];  //setting estimated reliabilities in the fixed case
		this.AssignedReliabilities[s][w]= initialRel[w];  //setting estimated reliabilities in the random case
//		}
	}
}
*/
//changing reliabilities in every simulation
initialRel=Reliabilities;

for(int s=0; s<this.numAgents; s++ )
{
	for(int w=0; w < this.numAgents; w++)
	{
			this.AssignedReliabilities[s][w]= Reliabilities[w];
	}
}
//-------------------------------13 possible rankings of three alternatives--------------------------------
List<String> l1= new ArrayList<String>();
List<String> l2= new ArrayList<String>();
List<String> l3= new ArrayList<String>();
List<String> l4= new ArrayList<String>();
List<String> l5= new ArrayList<String>();
List<String> l6= new ArrayList<String>();
List<String> l7= new ArrayList<String>();
List<String> l8= new ArrayList<String>();
List<String> l9= new ArrayList<String>();
List<String> l10= new ArrayList<String>();
List<String> l11= new ArrayList<String>();
List<String> l12= new ArrayList<String>();
List<String> l13= new ArrayList<String>();

l1.add(Options[0]);
l1.add(Options[1]);
l1.add(Options[2]);

l2.add(Options[0]);
l2.add(Options[2]);
l2.add(Options[1]);

l3.add(Options[1]);
l3.add(Options[0]);
l3.add(Options[2]);

l4.add(Options[1]);
l4.add(Options[2]);
l4.add(Options[0]);

l5.add(Options[2]);
l5.add(Options[0]);
l5.add(Options[1]);

l6.add(Options[2]);
l6.add(Options[1]);
l6.add(Options[0]);

l7.add(Options[0]);
l7.add(Options[5]);

l8.add(Options[1]);
l8.add(Options[4]);

l9.add(Options[2]);
l9.add(Options[3]);

l10.add(Options[3]);
l10.add(Options[2]);

l11.add(Options[4]);
l11.add(Options[1]);

l12.add(Options[5]);
l12.add(Options[0]);

l13.add(Options[6]);


//--------fixing the order of possible rankings generated above based on which the distance matrixes are calculated ---------------------------------



anchoring.Rankings[0]= l13;
anchoring.Rankings[1]= l10;
anchoring.Rankings[2]= l11;
anchoring.Rankings[3]= l12;
anchoring.Rankings[4]= l7;
anchoring.Rankings[5]= l8;
anchoring.Rankings[6]= l9;
anchoring.Rankings[7]= l1;
anchoring.Rankings[8]= l2;
anchoring.Rankings[9]= l3;
anchoring.Rankings[10]= l4;
anchoring.Rankings[11]= l5;
anchoring.Rankings[12]= l6;



anchoring.StrictRankings[0]= l1;
anchoring.StrictRankings[1]= l2;
anchoring.StrictRankings[2]= l3;
anchoring.StrictRankings[3]= l4;
anchoring.StrictRankings[4]= l5;
anchoring.StrictRankings[5]= l6;

}

//-------------given a ranking "l" and an alternative "input" returns the rank of the alternative in l----------------------------------------------------------------------------------------


public int IndexOf(int input, List<String> l)
{
	int index=1110; // a random initial value used later for error check
	List<String> givenList=l; // the given ranking
	int toBeChecked=input; // the given alternative
	int length= l.size();
	for(int i=0; i< length; i++)
	{
		int k=0;
		k=givenList.get(i).length();
		String[] eachLevel= new String[k];
		eachLevel=givenList.get(i).split("");
		for(int z=0; z< k; z++)
		{
			if(Objects.equals(eachLevel[z],String.valueOf(toBeChecked)))
			{
				index=i;
				break;
			}
		}
	}
	if(index==1110) System.out.println("error"); //for error checking
	return index+1;
}
//----------------------------------------------voting randomly-------------------------------------------------------
public int[] voting()
{
	int[] Votes=new int[this.numAgents];
	
	for(int i=0; i< this.numAgents;i++)
	{
		int randomNum = ThreadLocalRandom.current().nextInt(0, 13); //assigns to each agent a number between 0 and 12 representing the agents chosen ranking
		Votes[i]=randomNum;
	}	
	return Votes;
}

//----------------------------------------------strict voting randomly-------------------------------------------------------
public int[] Stvoting()
{
	int[] StVotes=new int[this.numAgents];
	
	for(int i=0; i< this.numAgents;i++)
	{
		int randomNum = ThreadLocalRandom.current().nextInt(0, 6); //assigns to each agent a number between 0 and 12 representing the agents chosen ranking
		StVotes[i]=randomNum+7;
	}
	return StVotes;
}




//----------------------------------------------voting-first person separated randomly-------------------------------------------------------
public int[] votingS()
{
	int[] Votes=new int[this.numAgents];
	int first;
	Votes[0]= ThreadLocalRandom.current().nextInt(0, 13);
	first= Votes[0];
	int j=1;
	while(j<this.numAgents)
	{
		int randomNum = ThreadLocalRandom.current().nextInt(0, 13); //assigns to each agent a number between 0 and 12 representing the agents chosen ranking
		if(randomNum!= first)
			{
			Votes[j]=randomNum;
			j=j+1;
			}
	}	
	return Votes;
}

//----------------------------------------------strict voting first person separated randomly-------------------------------------------------------
public int[] StvotingS()
{
	int[] StVotes=new int[this.numAgents];
	int first;
	StVotes[0]= ThreadLocalRandom.current().nextInt(0, 6);
	first= StVotes[0];
	int j=1;
	while(j<this.numAgents)
	{
		int randomNum = ThreadLocalRandom.current().nextInt(0, 6); //assigns to each agent a number between 0 and 12 representing the agents chosen ranking
		if(randomNum!= first)
			{
			StVotes[j]=randomNum;
			j=j+1;
			}
	}	
	return StVotes;
}




//-------------------------------------------------DP update----------------------------------------------------
//-----------Given two rankings vote1 and vote2 returns the ranking which minimizes the average DP distance to the vote1 and vote2 weighted by ther eliabilities

public int DPupdate(int vote1, int vote2, double rel1, double rel2)
{
	double[] DPAvDis= new double[13];     //to store average distances
	for(int i=0; i<13; i++)
	{
		DPAvDis[i]= Math.sqrt(rel1*java.lang.Math.pow(anchoring.DPDistanceMatrix[vote1][i],2)+rel2* java.lang.Math.pow(anchoring.DPDistanceMatrix[vote2][i],2));
	} //for each i sets the valueDPAvDis[i] to the average distance of the ranking number i to vote1 and vote2 weighted by the reliabilities	
	int counter= 0;
	for(int j=1; j<13; j++)     //finds the i with the minimal average distance to vote1 and vote2
	{
		if(DPAvDis[j]< DPAvDis[counter])
		{
			counter= j;
		}	
		if(DPAvDis[j]== DPAvDis[counter])
		{
			int randomNumber = ThreadLocalRandom.current().nextInt(0, 2);
			if(randomNumber==0)
			{
				counter= j;
			}
		}	
	}
	return counter;
}




//-------------------------------------------------DP Strict update----------------------------------------------------
//-----------Given two rankings vote1 and vote2 returns the ranking which minimizes the average DP distance to the vote1 and vote2 weighted by ther eliabilities

public int DPStupdate(int vote1, int vote2, double rel1, double rel2)
{
	double[] DPStAvDis= new double[6];     //to store average distances
	for(int i=0; i<6; i++)
	{
		DPStAvDis[i]= Math.sqrt(rel1*java.lang.Math.pow(anchoring.DPDistanceMatrix[vote1][i+7],2)+rel2* java.lang.Math.pow(anchoring.DPDistanceMatrix[vote2][i+7],2));	
	} //for each i sets the valueDPAvDis[i] to the average distance of the ranking number i to vote1 and vote2 weighted by the reliabilities	
	int counter= 0;
	for(int j=1; j<6; j++)     //finds the i with the minimal average distance to vote1 and vote2
	{
		if(DPStAvDis[j]< DPStAvDis[counter])
		{
			counter= j;
		}
		if(DPStAvDis[j]== DPStAvDis[counter])
		{
			int randomNumber = ThreadLocalRandom.current().nextInt(0, 2);
			if(randomNumber==0)
			{
				counter= j;
			}	
		}	
	}
	return counter+7;
}



//-------------------------------------------------KS update----------------------------------------------------
//-----------Given two rankings vote1 and vote2 returns the ranking which minimizes the average KS distance to the vote1 and vote2 weighted by ther eliabilities


public int KSupdate(int vote1, int vote2, double rel1, double rel2)
{
	double[] KSAvDis= new double[13]; //to store average distances
	for(int i=0; i<13; i++)
	{
		KSAvDis[i]= Math.sqrt(rel1*java.lang.Math.pow(anchoring.KDistanceMatrix[vote1][i],2)+rel2* java.lang.Math.pow(anchoring.KDistanceMatrix[vote2][i],2));
	} //for each i sets the value KSAvDis[i] to the average distance of the ranking number i to vote1 and vote2 weighted by the reliabilities	
	int counter= 0;
	for(int j=1; j<13; j++) //finds the i with the minimal average distance to vote1 and vote2
	{
		if(KSAvDis[j]< KSAvDis[counter])
		{
			counter= j;
		}
		if(KSAvDis[j]== KSAvDis[counter])
		{
			int randomNumber = ThreadLocalRandom.current().nextInt(0, 2);
			if(randomNumber==0)
			{
				counter= j;
			}
		}	
	}
	return counter;
}

//-------------------------------------------------KS Strict update---------------------------------------------------
//-----------Given two rankings vote1 and vote2 returns the ranking which minimizes the average KS distance to the vote1 and vote2 weighted by ther eliabilities


public int KSStupdate(int vote1, int vote2, double rel1, double rel2)
{
	double[] KSStAvDis= new double[6]; //to store average distances
	for(int i=0; i<6; i++)
	{
		KSStAvDis[i]= Math.sqrt(rel1*java.lang.Math.pow(anchoring.KDistanceMatrix[vote1][i+7],2)+rel2* java.lang.Math.pow(anchoring.KDistanceMatrix[vote2][i+7],2));
	} //for each i sets the value KSAvDis[i] to the average distance of the ranking number i to vote1 and vote2 weighted by the reliabilities	
	int counter= 0;
	for(int j=1; j<6; j++) //finds the i with the minimal average distance to vote1 and vote2
	{
		if(KSStAvDis[j]< KSStAvDis[counter])
		{
			counter= j;
		}
		if(KSStAvDis[j]== KSStAvDis[counter])
		{
			int randomNumber = ThreadLocalRandom.current().nextInt(0, 2);
			if(randomNumber==0)
			{
				counter= j;
			}
		}
	}
	return counter+7;
}

//-------------------------------------------------CS update----------------------------------------------------
//-----------Given two rankings vote1 and vote2 returns the ranking which minimizes the average CS distance to the vote1 and vote2 weighted by the reliabilities


public int CSupdate(int vote1, int vote2, double rel1, double rel2)
{
	double[] CSAvDis= new double[13]; //to store average distances
	for(int i=0; i<13; i++)
	{
		CSAvDis[i]= Math.sqrt(rel1*java.lang.Math.pow(anchoring.CSDistanceMatrix[vote1][i],2)+rel2* java.lang.Math.pow(anchoring.CSDistanceMatrix[vote2][i],2));
	} //for each i sets the value KSAvDis[i] to the average distance of the ranking number i to vote1 and vote2 weighted by the reliabilities
	int counter= 0;
	for(int j=1; j<13; j++) //finds the i with the minimal average distance to vote1 and vote
	{
		if(CSAvDis[j]< CSAvDis[counter])
		{
			counter= j;
		}
		if(CSAvDis[j]== CSAvDis[counter])
		{
			int randomNumber = ThreadLocalRandom.current().nextInt(0, 2);
			if(randomNumber==0)
			{
				counter= j;
			}
		}
	}
	return counter;
}


//-------------------------------------------------CS strict update----------------------------------------------------
//-----------Given two rankings vote1 and vote2 returns the ranking which minimizes the average CS distance to the vote1 and vote2 weighted by the reliabilities


public int CSStupdate(int vote1, int vote2, double rel1, double rel2)
{
	double[] CSStAvDis= new double[6]; //to store average distances
	for(int i=0; i<6; i++)
	{
		CSStAvDis[i]= Math.sqrt(rel1*java.lang.Math.pow(anchoring.CSDistanceMatrix[vote1][i+7],2)+rel2* java.lang.Math.pow(anchoring.CSDistanceMatrix[vote2][i+7],2));
	} //for each i sets the value KSAvDis[i] to the average distance of the ranking number i to vote1 and vote2 weighted by the reliabilities	
	int counter= 0;
	for(int j=1; j<6; j++) //finds the i with the minimal average distance to vote1 and vote
	{
		if(CSStAvDis[j]< CSStAvDis[counter])
		{
			counter= j;
		}
		if(CSStAvDis[j]== CSStAvDis[counter])
		{	
			int randomNumber = ThreadLocalRandom.current().nextInt(0, 2);
			if(randomNumber==0)
			{
				counter= j;
			}
		}	
	}
	return counter+7;
}

//-----------------------------------------------------Deliberation with DP distance--fixed order---------------------------------------------

public int[] DelibDP(int[] votes)
{
	int[][] DelibRoundsResults= new int[this.numDelibRounds+1][this.numAgents];
	int[][] eachRoundResults= new int[this.numAgents+1][this.numAgents];
	for(int k=0; k< this.numAgents; k++)
	{
		DelibRoundsResults[0][k]= votes[k];
	}
	
	
	for(int i=1; i<numDelibRounds+1; i++)
	{	
		int[] LastRoundResults=new int[this.numAgents];
		for(int w=0; w< this.numAgents; w++)
		{			
			LastRoundResults[w]=DelibRoundsResults[i-1][w];
		}	
		for(int j=0; j< this.numAgents+1; j++)
		{	
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= 1000;
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			eachRoundResults[0][w]=LastRoundResults[w];
		}
		for(int j=1; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= DPupdate(eachRoundResults[j-1][j-1],eachRoundResults[j-1][l],  ((double)this.AssignedReliabilities[l][j-1]),  1-((double)this.AssignedReliabilities[l][j-1]));
			}
		}	
		for(int w=0; w< this.numAgents; w++)
		{
			DelibRoundsResults[i][w]=eachRoundResults[this.numAgents][w];
		}
	}
	return DelibRoundsResults[numDelibRounds];
}

//-----------------------------------------------------Deliberation with DP distance on strict orders-----fixed order--------------------------------------------

public int[] DelibStDP(int[] votes)    //takes as input a set of integers between 0-12 representing the agents chosen ranking
{
	int[][] DelibRoundsResults= new int[this.numDelibRounds+1][this.numAgents];
	int[][] eachRoundResults= new int[this.numAgents+1][this.numAgents];
	for(int k=0; k< this.numAgents; k++)
	{
		DelibRoundsResults[0][k]= votes[k];
	}	
	for(int i=1; i<numDelibRounds+1; i++)
	{
		int[] LastRoundResults=new int[this.numAgents];
		for(int w=0; w< this.numAgents; w++)
		{
			LastRoundResults[w]=DelibRoundsResults[i-1][w];
		}	
		for(int j=0; j< this.numAgents+1; j++)
		{	
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= 1000;
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			eachRoundResults[0][w]=LastRoundResults[w];
		}
		for(int j=1; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= DPStupdate(eachRoundResults[j-1][j-1],eachRoundResults[j-1][l],  ((double)this.AssignedReliabilities[l][j-1]),  1-((double)this.AssignedReliabilities[l][j-1]));
 			}
		}	
		for(int w=0; w< this.numAgents; w++)
		{
			DelibRoundsResults[i][w]=eachRoundResults[this.numAgents][w];
		}
	}
	return DelibRoundsResults[numDelibRounds];
}

//-----------------------------------------------------Deliberation with DP distance random order------------------------------------------------

public int[] DelibDPRandom(int[] votes)
{
	int[][] DelibRoundsResults= new int[this.numDelibRounds+1][this.numAgents];
	int[][] eachRoundResults= new int[this.numAgents+1][this.numAgents];
	int[] Agents= new int[this.numAgents];
	
	for(int o=0; o< this.numAgents; o++)
	{
		Agents[o]=o;
	}
	for(int k=0; k< this.numAgents; k++)
	{
		DelibRoundsResults[0][k]= votes[k];
	}
	
	for(int i=1; i<numDelibRounds+1; i++)
	{
		int index, temp; //randomising agents
		Random random = new Random();
		for (int l = this.numAgents - 1; l > 0; l--)
		{
			index = random.nextInt(l + 1);
			temp = Agents[index];
			Agents[index] = Agents[l];
			Agents[l] = temp;
		}
	
		int[] LastRoundResults=new int[this.numAgents];
		for(int w=0; w< this.numAgents; w++)
		{
			LastRoundResults[w]=DelibRoundsResults[i-1][w];
		}
		for(int j=0; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= 1000;
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			eachRoundResults[0][w]=LastRoundResults[w];
		}
	
		for(int j=1; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= DPupdate(eachRoundResults[j-1][Agents[j-1]],eachRoundResults[j-1][l], ((double)this.AssignedReliabilities[l][Agents[j-1]]), 1- ((double) this.AssignedReliabilities[l][Agents[j-1]]));
				//updating using DP update for each agent after receiving the opinion of the speaker
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			DelibRoundsResults[i][w]=eachRoundResults[this.numAgents][w];
			//the result of updates at the end of round i

		}
	}
	return DelibRoundsResults[numDelibRounds];
}
//-----------------------------------------------Deliberation with DP-random order strict rankings--------------------------------------------------
public int[] DelibStDPRandom(int[] votes)
{
	int[][] DelibRoundsResults= new int[this.numDelibRounds+1][this.numAgents];
	int[][] eachRoundResults= new int[this.numAgents+1][this.numAgents];
	int[] Agents= new int[this.numAgents];
	
	for(int o=0; o< this.numAgents; o++)
	{
		Agents[o]=o;
	}
	for(int k=0; k< this.numAgents; k++)
	{
		DelibRoundsResults[0][k]= votes[k];
	}
	
	for(int i=1; i<numDelibRounds+1; i++)
	{
		int index, temp; //randomising agents
		Random random = new Random();
		for (int l = this.numAgents - 1; l > 0; l--)
		{
			index = random.nextInt(l + 1);
			temp = Agents[index];
			Agents[index] = Agents[l];
			Agents[l] = temp;
		}
	
		int[] LastRoundResults=new int[this.numAgents];
		for(int w=0; w< this.numAgents; w++)
		{
			LastRoundResults[w]=DelibRoundsResults[i-1][w];
		}
		for(int j=0; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= 1000;
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			eachRoundResults[0][w]=LastRoundResults[w];
		}
	
		for(int j=1; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= DPStupdate(eachRoundResults[j-1][Agents[j-1]],eachRoundResults[j-1][l], ((double)this.AssignedReliabilities[l][Agents[j-1]]),  1- ((double)this.AssignedReliabilities[l][Agents[j-1]]));
				//updating using DP strict update after receiving the opinion of the speaker
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			DelibRoundsResults[i][w]=eachRoundResults[this.numAgents][w];
			// the result of updates at the end of round i
		}
	}
	return DelibRoundsResults[numDelibRounds];
}
//----------------------------------------------------Deliberation KS fixed order-------------------------------------------------
public int[] DelibKS(int[] votes)
{
	int[][] DelibRoundsResults= new int[this.numDelibRounds+1][this.numAgents];
	int[][] eachRoundResults= new int[this.numAgents+1][this.numAgents];
	for(int k=0; k< this.numAgents; k++)
	{
	DelibRoundsResults[0][k]= votes[k];
	}	
	for(int i=1; i<numDelibRounds+1; i++)
	{
		int[] LastRoundResults=new int[this.numAgents];
		for(int w=0; w< this.numAgents; w++)
		{
			LastRoundResults[w]=DelibRoundsResults[i-1][w];
		}
		for(int j=0; j< this.numAgents+1; j++)
		{		
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= 1000;
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			eachRoundResults[0][w]=LastRoundResults[w];
		}
		for(int j=1; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= KSupdate(eachRoundResults[j-1][j-1],eachRoundResults[j-1][l],  ((double)this.AssignedReliabilities[l][j-1]),  1-((double)this.AssignedReliabilities[l][j-1]));
 			}
		}		
		for(int w=0; w< this.numAgents; w++)
		{
			DelibRoundsResults[i][w]=eachRoundResults[this.numAgents][w];
		}
	}
return DelibRoundsResults[numDelibRounds];
}
//----------------------------------------------------Deliberation KS fixed order strict rankings-------------------------------------------------
public int[] DelibStKS(int[] votes)
{
	int[][] DelibRoundsResults= new int[this.numDelibRounds+1][this.numAgents];
	int[][] eachRoundResults= new int[this.numAgents+1][this.numAgents];	
	for(int k=0; k< this.numAgents; k++)
	{
		DelibRoundsResults[0][k]= votes[k];
	}
	
	for(int i=1; i<numDelibRounds+1; i++)
	{	
		int[] LastRoundResults=new int[this.numAgents];
		for(int w=0; w< this.numAgents; w++)
		{
			LastRoundResults[w]=DelibRoundsResults[i-1][w];
		}			
		for(int j=0; j< this.numAgents+1; j++)
		{
		
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= 1000;
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			eachRoundResults[0][w]=LastRoundResults[w];
		}
		for(int j=1; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= KSStupdate(eachRoundResults[j-1][j-1],eachRoundResults[j-1][l],  ((double)this.AssignedReliabilities[l][j-1]),  1-((double)this.AssignedReliabilities[l][j-1]));
 			}
		}		
		for(int w=0; w< this.numAgents; w++)
		{
			DelibRoundsResults[i][w]=eachRoundResults[this.numAgents][w];
		}
	}
return DelibRoundsResults[numDelibRounds];
}


//-----------------------------------------------Deliberation with KS-random order-----------------------------------------------------
public int[] DelibKSRandom(int[] votes)
{
	int[][] DelibRoundsResults= new int[this.numDelibRounds+1][this.numAgents];
	int[][] eachRoundResults= new int[this.numAgents+1][this.numAgents];
	int[] Agents= new int[this.numAgents];
	
	for(int o=0; o< this.numAgents; o++)
	{
		Agents[o]=o;
	}
	for(int k=0; k< this.numAgents; k++)
	{
		DelibRoundsResults[0][k]= votes[k];
	}
	
	for(int i=1; i<numDelibRounds+1; i++)
	{
		int index, temp; //randomising agents
		Random random = new Random();
		for (int l = this.numAgents - 1; l > 0; l--)
		{
			index = random.nextInt(l + 1);
		    temp = Agents[index];
		    Agents[index] = Agents[l];
		    Agents[l] = temp;
		}
		int[] LastRoundResults=new int[this.numAgents];
		for(int w=0; w< this.numAgents; w++)
		{
			LastRoundResults[w]=DelibRoundsResults[i-1][w];
		}		
		for(int j=0; j< this.numAgents+1; j++)
		{	
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= 1000;
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			eachRoundResults[0][w]=LastRoundResults[w];
		}
		for(int j=1; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= KSupdate(eachRoundResults[j-1][Agents[j-1]],eachRoundResults[j-1][l],  ((double) this.AssignedReliabilities[l][Agents[j-1]]),  1- ((double) this.AssignedReliabilities[l][Agents[j-1]]));
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			DelibRoundsResults[i][w]=eachRoundResults[this.numAgents][w];
		}
	}	
	return DelibRoundsResults[numDelibRounds];
}

//-----------------------------------------------Deliberation with KS-random order---strict rankings--------------------------------------------------
public int[] DelibStKSRandom(int[] votes)
{
	int[][] DelibRoundsResults= new int[this.numDelibRounds+1][this.numAgents];
	int[][] eachRoundResults= new int[this.numAgents+1][this.numAgents];
	int[] Agents= new int[this.numAgents];	
	for(int o=0; o< this.numAgents; o++)
	{
	Agents[o]=o;
	}
	for(int k=0; k< this.numAgents; k++)
	{
	DelibRoundsResults[0][k]= votes[k];
	}
	
	for(int i=1; i<numDelibRounds+1; i++)
	{
		int index, temp; //randomising agents
		   Random random = new Random();
		   for (int l = this.numAgents - 1; l > 0; l--)
		   {
		       index = random.nextInt(l + 1);
		       temp = Agents[index];
		       Agents[index] = Agents[l];
		       Agents[l] = temp;
		   }	
		 int[] LastRoundResults=new int[this.numAgents];
		 for(int w=0; w< this.numAgents; w++)
		 {
			 LastRoundResults[w]=DelibRoundsResults[i-1][w];
		 }		
		 for(int j=0; j< this.numAgents+1; j++)
		 {
			 for(int l=0; l< this.numAgents; l++)
			 {
				 eachRoundResults[j][l]= 1000;
			 }
		}
		for(int w=0; w< this.numAgents; w++)
		{
			eachRoundResults[0][w]=LastRoundResults[w];
		}
		for(int j=1; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= KSStupdate(eachRoundResults[j-1][Agents[j-1]],eachRoundResults[j-1][l], ((double) this.AssignedReliabilities[l][Agents[j-1]]),  1- ((double) this.AssignedReliabilities[l][Agents[j-1]]));
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			DelibRoundsResults[i][w]=eachRoundResults[this.numAgents][w];
		}
	}	
	return DelibRoundsResults[numDelibRounds];
}

//----------------------------------------------------Deliberation CS fixed order-------------------------------------------------
public int[] DelibCS(int[] votes)
{
	int[][] DelibRoundsResults= new int[this.numDelibRounds+1][this.numAgents];
	int[][] eachRoundResults= new int[this.numAgents+1][this.numAgents];	
	for(int k=0; k< this.numAgents; k++)
	{
		DelibRoundsResults[0][k]= votes[k];
	}	
	for(int i=1; i<numDelibRounds+1; i++)
	{	
		int[] LastRoundResults=new int[this.numAgents];
		for(int w=0; w< this.numAgents; w++)
		{
			LastRoundResults[w]=DelibRoundsResults[i-1][w];
		}
		for(int j=0; j< this.numAgents+1; j++)
		{	
			for(int l=0; l< this.numAgents; l++)
			{
			eachRoundResults[j][l]= 1000;
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			eachRoundResults[0][w]=LastRoundResults[w];
		}
		for(int j=1; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= CSupdate(eachRoundResults[j-1][j-1],eachRoundResults[j-1][l],  ((double)this.AssignedReliabilities[l][j-1]),  1-((double)this.AssignedReliabilities[l][j-1]));
 			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			DelibRoundsResults[i][w]=eachRoundResults[this.numAgents][w];
		}
	}	
	return DelibRoundsResults[numDelibRounds];
}
//----------------------------------------------------Deliberation CS fixed order strict rankings-----------------------------------------------
public int[] DelibStCS(int[] votes)
{
	int[][] DelibRoundsResults= new int[this.numDelibRounds+1][this.numAgents];
	int[][] eachRoundResults= new int[this.numAgents+1][this.numAgents];
	for(int k=0; k< this.numAgents; k++)
	{
		DelibRoundsResults[0][k]= votes[k];
	}	
	for(int i=1; i<numDelibRounds+1; i++)
	{
		int[] LastRoundResults=new int[this.numAgents];
		for(int w=0; w< this.numAgents; w++)
		{
			LastRoundResults[w]=DelibRoundsResults[i-1][w];
		}	
		for(int j=0; j< this.numAgents+1; j++)
		{		
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= 1000;
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			eachRoundResults[0][w]=LastRoundResults[w];
		}
		for(int j=1; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= CSStupdate(eachRoundResults[j-1][j-1],eachRoundResults[j-1][l],  ((double)this.AssignedReliabilities[l][j-1]),  1-((double)this.AssignedReliabilities[l][j-1]));
			}
		}	
		for(int w=0; w< this.numAgents; w++)
		{
			DelibRoundsResults[i][w]=eachRoundResults[this.numAgents][w];
			//the result of updates at the end of round i	
		}
	}	
	return DelibRoundsResults[numDelibRounds];
}

//-----------------------------------------------------Deliberation with CS distance random order------------------------------------------------

public int[] DelibCSRandom(int[] votes)
{
	int[][] DelibRoundsResults= new int[this.numDelibRounds+1][this.numAgents];
	int[][] eachRoundResults= new int[this.numAgents+1][this.numAgents];
	int[] Agents= new int[this.numAgents];
	
	for(int o=0; o< this.numAgents; o++)
	{
		Agents[o]=o;
	}
	for(int k=0; k< this.numAgents; k++)
	{
		DelibRoundsResults[0][k]= votes[k];
	}
	
	for(int i=1; i<numDelibRounds+1; i++)
	{
		int index, temp; //randomising agents
		Random random = new Random();
		for (int l = this.numAgents - 1; l > 0; l--)
		{
			index = random.nextInt(l + 1);
			temp = Agents[index];
			Agents[index] = Agents[l];
			Agents[l] = temp;
		}
	
		int[] LastRoundResults=new int[this.numAgents];
		for(int w=0; w< this.numAgents; w++)
		{
			LastRoundResults[w]=DelibRoundsResults[i-1][w];
		}
		for(int j=0; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= 1000;
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			eachRoundResults[0][w]=LastRoundResults[w];
		}
	
		for(int j=1; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= CSupdate(eachRoundResults[j-1][Agents[j-1]],eachRoundResults[j-1][l], ((double)this.AssignedReliabilities[l][Agents[j-1]]), 1- ((double) this.AssignedReliabilities[l][Agents[j-1]]));
				//updating using CS update for each agent after receiving the opinion of the speaker
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			DelibRoundsResults[i][w]=eachRoundResults[this.numAgents][w];
			//the result of updates at the end of round i

		}
	}
	return DelibRoundsResults[numDelibRounds];
}
//-----------------------------------------------Deliberation with CS-random order strict rankings--------------------------------------------------
public int[] DelibStCSRandom(int[] votes)
{
	int[][] DelibRoundsResults= new int[this.numDelibRounds+1][this.numAgents];
	int[][] eachRoundResults= new int[this.numAgents+1][this.numAgents];
	int[] Agents= new int[this.numAgents];
	
	for(int o=0; o< this.numAgents; o++)
	{
		Agents[o]=o;
	}
	for(int k=0; k< this.numAgents; k++)
	{
		DelibRoundsResults[0][k]= votes[k];
	}
	
	for(int i=1; i<numDelibRounds+1; i++)
	{
		int index, temp; //randomising agents
		Random random = new Random();
		for (int l = this.numAgents - 1; l > 0; l--)
		{
			index = random.nextInt(l + 1);
			temp = Agents[index];
			Agents[index] = Agents[l];
			Agents[l] = temp;
		}
	
		int[] LastRoundResults=new int[this.numAgents];
		for(int w=0; w< this.numAgents; w++)
		{
			LastRoundResults[w]=DelibRoundsResults[i-1][w];
		}
		for(int j=0; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= 1000;
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			eachRoundResults[0][w]=LastRoundResults[w];
		}
	
		for(int j=1; j< this.numAgents+1; j++)
		{
			for(int l=0; l< this.numAgents; l++)
			{
				eachRoundResults[j][l]= CSStupdate(eachRoundResults[j-1][Agents[j-1]],eachRoundResults[j-1][l],  ((double)this.AssignedReliabilities[l][Agents[j-1]]),  1- ((double)this.AssignedReliabilities[l][Agents[j-1]]));
				//updating using CS strict update after receiving the opinion of the speaker
			}
		}
		for(int w=0; w< this.numAgents; w++)
		{
			DelibRoundsResults[i][w]=eachRoundResults[this.numAgents][w];
			// the result of updates at the end of round i
		}
	}
	return DelibRoundsResults[numDelibRounds];
}

//-------------------------------------------------------checks how many agents weakly prefer alternative i to alternative j in profile vote------------------------------------------------


public int pairwiseWPrefered(int i, int j,int[] vote)
{
	int count=0;
	for(int k=0; k <vote.length; k++)
	{
		if(this.IndexOf(i, anchoring.Rankings[vote[k]]) <= this.IndexOf(j, anchoring.Rankings[vote[k]])) //adds one to the counter for each agent that has ranked i higher than j
		{
			count=count+1;
		}
	}
	return count;
}


//-----------------------------------------------weak compares alternative in pairwise majority --------------------------------------------------------------------------------------------------

public boolean[][] pairwiseWcomparison(int[] vote)
{
	boolean[][] matrix= new boolean[3][3];     //the comparison matrix
	for(int k=0; k <3; k++)
	{
		for(int m=0; m <3; m++)
		{
			if(pairwiseWPrefered(k+1,m+1,vote) > (this.numAgents/2))  //sets entry (i, j) to true if a majority prefers i to j and false otherwise
			{
				matrix[k][m] =true;
			}else matrix[k][m]=false;
		}
	}
	return matrix;
}
//--------------------------------------------------------------checks for cycles----------------------------------------------------------------------------------------------

public boolean checkforCycles(int[] vote)
{
	boolean result=false;
	boolean[][] matrix= new boolean[3][3];
	matrix=this.pairwiseWcomparison(vote);
	int counter=0;
	for(int k=0; k <3; k++)
	{
		for(int m=0; m <3; m++)
		{
			for(int n=0; n <3; n++)
			{
				if(matrix[k][m] == true & matrix[m][k] == false & matrix[m][n]== true & matrix[n][m] == false & matrix[n][k]==true)  //checks if there are i,j and k that form a cycle in pairwise majority voting
				{
					result= true;
					counter=counter+1;
				}
			}
		}
	}
	return result;
}

//--------------------------------------checks for non transitivity----------------------------------------------------------------------------------------------

public boolean checkforNtran(int[] vote)
{
	boolean result=false;
	boolean[][] matrix= new boolean[3][3];
	matrix=this.pairwiseWcomparison(vote);
	for(int k=0; k <3; k++)
	{
		for(int m=0; m <3; m++)
		{
			for(int n=0; n <3; n++)
			{
				if(matrix[k][m] == true & matrix[m][n]== true & matrix[k][n]==false) //checks if there are i,j and k that create non-transitivity
				{
					result=true;
				}
			}
		}
	}
	return result;
}
//--------------------------------------------------------------checks for clusters----------------------------------------------------------------------------------------------

public static int[] clustersizes(int[] votes)
{
	int size= votes.length;
	int[] clusters=new int[13];
	for(int j=0; j<13; j++)
	{
		clusters[j]=0;
	}
	for(int i=0; i<size; i++)
	{
		clusters[votes[i]]= clusters[votes[i]]+1;
	}
	return clusters;

}
//--------------------------------------------------------------cluster counting----------------------------------------------------------------------------------------------


public int clustercounting(int[] clusters) //counting how many in each cluster
{
	int size= clusters.length;
	int count=0;
	for(int j=0; j<size; j++)
	{
		if(clusters[j]!=0)
		{
			count= count+1;
		}
	}
	
return count;
}

//--------------------------------------------------------------counting clusters ----------------------------------------------------------------------------------------------

public int countcluster(int[] votes)
{
	
	int clustercount=1;
	Set<Integer> setvote = new HashSet<Integer>();
	for (Integer x : votes)
	{  
		setvote.add(x);
		clustercount=setvote.size();
	}
	return clustercount;
}

//--------------------checks if the final profile after deliberation is in consensus----------------------------------------------------

public boolean checkconsensus(int[] votes)
{
	int[] values=votes;
	for(int i=1; i< this.numAgents; i++)
	{
		if(values[i] != values[0])
			return false;
	}
	return true;
}

//-------------------------------------------------------converting string values to integers----------------------------------------------

public int StringtoInteger(String s)
{
	int u=0;
	if(s.equals("1")) u=1;
	if(s.equals("2")) u=2;
	if(s.equals("3")) u=3;
	if(s.equals("4")) u=4;
	if(s.equals("5")) u=5;
return u;
}

//-------------------------------------------------------converting integer values to strings----------------------------------------------

public String IntegerToString(int s)
{
	String u = new String();	
	if(s==1) u="1";
	if(s==2) u="2";
	if(s==3) u="3";
	if(s==4) u="4";
	if(s==5) u="5";
return u;
}

//------------------------------------------------checks if there is anchoring by first speaker--------------------------


public boolean checkAnchoring(double[] distances) //the input is an array in which the i-th element is the distances between
{ //the initial vote of i-th speaker to the final profile after deliberation
//and check if the first entry is the smallest
double[] values=distances;
for(int i=0; i< values.length; i++)
{
if(values[i] < values[0])
return false;
}
return true;
}

//--------------------------------------------for test----checks if there is anchoring by first speaker initial votes--------------------------


public boolean checkiniAnchoring(double[] distances) //the input is an array in which the i-th element is the distances between
{ //the initial vote of i-th speaker to the final profile after deliberation
//and check if the first entry is the smallest
//int result=0;
double[] values=distances;
for(int i=0; i< values.length; i++)
{
	if(values[i] < values[0]) 
	{
		//result=1;
		return true;
	}
}
//return result;
return false;
}
//--------------------------------------------for test----checks if there is double anchoring by first speaker and some one else--------------------------

public int checkiniAnchoring2(double[] distances, int[] votes) //the input is an array in which the i-th element is the distances between
{ //the initial vote of i-th speaker to the final profile after deliberation
//and check if the first entry is the smallest
int result=0;
boolean check=true;
double[] values=distances;
for(int i=0; i< values.length; i++)
{
	if(values[i] < values[0])
	check=false;
}
if(check)
{
	for(int i=0; i< values.length; i++)
	{
		if(values[i] == values[0])
		{
			if(votes[i]!=votes[0])
			{
				result=1;
			}
		}
	}
}
return result;
}

//------------------------------------------------checks if there is anchoring by the expert--------------------------


public boolean checkAnchoringEx(double[] distances, int exp) 
{
double[] values=distances;
for(int i=0; i< values.length; i++)
{
if(values[i] < values[exp])
return false;
}
return true;
}
//------------------------------------------------finding the expert (highest reliability)--------------------------

public int expert()
{
  int largest = 0;
  for ( int i = 1; i < this.initialRel.length; i++ )
  {
      if ( this.initialRel[i] > this.initialRel[largest] ) largest = i;
  }
  return largest; // position of the expert
}
//------------------------------------------checks if a given ranking is single peaked with respect to the ordering "1,2,3" of the alternatives


public boolean singlePeaked(List<String> vote)
{
	if(vote.size()<=2) //if the ranking is not strict it is not single-peaked
	{
		return false;
	}else
	{
		int u=StringtoInteger(vote.get(0)); //taking the alternative ranked highest
		for(int i=1;i<u;i++)   //checking the correct order of rankings for alternatives appearing before the one ranked highest in "vote" (in the dimension "1, 2, 3")  
		{
			for(int j=1; j<i;j++)
			{
				if(i!=j)
				{
					if(IndexOf(i, vote)> IndexOf(j, vote))
					{
						return false;
					}
				}
			}
		}
		for(int i=u+1;i<=3;i++) //checking the correct order of rankings for alternatives appearing after the one ranked highest in "vote" (in the dimension "1, 2, 3")
		{
			for(int j=i; j<=3;j++)
			{
				if(i!=j)
				{
					if(IndexOf(i, vote)> IndexOf(j, vote))
					{
						return false;
					}
				}
			}
		}
	}
	return true;
}

//------------------------------------------checks if a given ranking is weakly single peaked with respect to the ordering "1,2,3" of the alternatives

public boolean WsinglePeaked(List<String> vote)
{
	
	if(vote.equals(anchoring.Rankings[2]))
	{
		return false;
	}else if(vote.equals(anchoring.Rankings[8]))
	{
		return false;
	}else if(vote.equals(anchoring.Rankings[11]))
	{
		return false;
	}else {
		return true;
	}
}

//------------------------------------------checks if a given ranking is single plateau with respect to the ordering "1,2,3" of the alternatives

public boolean SinglePlateau(List<String> vote)
{

	if(vote.equals(anchoring.Rankings[0]))
	{
		return false;
	}else if(vote.equals(anchoring.Rankings[2]))
	{
		return false;
	}else if(vote.equals(anchoring.Rankings[4]))
	{
		return false;
	}else if(vote.equals(anchoring.Rankings[6]))
	{
		return false;
	}else if(vote.equals(anchoring.Rankings[8]))
	{
		return false;
	}else if(vote.equals(anchoring.Rankings[11]))
	{
		return false;
	}else{
		return true;
		}
}


//------------------------------checks if a rankings is single peaked with respect to the given order "ref" assuming that ref is antisymmetric---------------------------------------

public boolean GsinglePeaked(int vote, int ref)
{
	List<String> toCheck= new ArrayList<String>(); //creating an array and initializing it to some arbitrary initial values
	toCheck.add(0, "100");
	toCheck.add(1, "100");
	toCheck.add(2, "100");
	
	if(anchoring.Rankings[vote].size()< 3) //if two alternatives are ranked the same then it is not single peaked
	{
		return false;
	}else
	{
	
	//take the given order ref and call the first alternative in the order "1", the second "2" and the third "3"
	//in the ranking vote make the same renaming
	int e1=StringtoInteger(anchoring.Rankings[ref].get(0));   //takes the first alternative in the given order called e1
	toCheck.set(this.IndexOf(e1, anchoring.Rankings[vote])-1, "1"); //checks the rank of e1 in the given ranking and set the same rank for "1" in the new ranking toCheck
	int e2=StringtoInteger(anchoring.Rankings[ref].get(1));
	toCheck.set(this.IndexOf(e2, anchoring.Rankings[vote])-1, "2");
	int e3=StringtoInteger(anchoring.Rankings[ref].get(2));
	toCheck.set(this.IndexOf(e3, anchoring.Rankings[vote])-1, "3");
	}
	
	if(this.singlePeaked(toCheck)) //check the single peakedness of the the created ranking with respect to the canonical ordering 1,2,3
	{
		return true;
	}else return false;
}

//------------------------------checks if a rankings is weakly single peaked with respect to the given order "ref" assuming that ref is antisymmetric---------------------------------------

public boolean GWsinglePeaked(int vote, int ref)
{
	List<String> toCheck= new ArrayList<String>(); //creating an array and initializing it to some arbitrary initial values
	List<String> toCheckf= new ArrayList<String>();
	
	for(int i=0; i<anchoring.Rankings[vote].size(); i++)
	{
		toCheck.add(i, "x");
	} //initialising the array to the right length
	
	
	//take the given order ref and call the first alternative in the order "1", the second "2" and the third "3"
	//in the ranking vote make the same renaming
		
	int e1=StringtoInteger(anchoring.Rankings[ref].get(0));   //takes the first alternative in the given order called e1
	toCheck.set(this.IndexOf(e1, anchoring.Rankings[vote])-1, toCheck.get(this.IndexOf(e1, anchoring.Rankings[vote])-1)+"1"); //checks the rank of e1 in the given ranking and set the same rank for "1" in the new ranking toCheck
	int e2=StringtoInteger(anchoring.Rankings[ref].get(1));
	toCheck.set(this.IndexOf(e2, anchoring.Rankings[vote])-1, toCheck.get(this.IndexOf(e2, anchoring.Rankings[vote])-1)+"2");
	int e3=StringtoInteger(anchoring.Rankings[ref].get(2));
	toCheck.set(this.IndexOf(e3, anchoring.Rankings[vote])-1, toCheck.get(this.IndexOf(e3, anchoring.Rankings[vote])-1)+"3");
		
	for(int e=0; e<toCheck.size(); e++)
	{
		toCheckf.add(e, toCheck.get(e).substring(1));
	}

	if(this.WsinglePeaked(toCheckf)) //check the single peakedness of the the created ranking with respect to the canonical ordering 1,2,3
	{
		return true;
	}else return false;
}


//------------------------------checks if a rankings is single plateau with respect to the given order "ref" assuming that ref is antisymmetric---------------------------------------

public boolean GSinglePlateau(int vote, int ref)
{
	List<String> toCheck= new ArrayList<String>(); //creating an array and initializing it to some arbitrary initial values
	List<String> toCheckf= new ArrayList<String>();
	
	for(int i=0; i<anchoring.Rankings[vote].size(); i++)
	{
		toCheck.add(i, "x");
	} //initialising the array to the right length
	
	//take the given order ref and call the first alternative in the order "1", the second "2" and the third "3"
	//in the ranking vote make the same renaming
	
	int e1=StringtoInteger(anchoring.Rankings[ref].get(0));   //takes the first alternative in the given order called e1
	toCheck.set(this.IndexOf(e1, anchoring.Rankings[vote])-1, toCheck.get(this.IndexOf(e1, anchoring.Rankings[vote])-1)+"1"); //checks the rank of e1 in the given ranking and set the same rank for "1" in the new ranking toCheck
	int e2=StringtoInteger(anchoring.Rankings[ref].get(1));
	toCheck.set(this.IndexOf(e2, anchoring.Rankings[vote])-1, toCheck.get(this.IndexOf(e2, anchoring.Rankings[vote])-1)+"2");
	int e3=StringtoInteger(anchoring.Rankings[ref].get(2));
	toCheck.set(this.IndexOf(e3, anchoring.Rankings[vote])-1, toCheck.get(this.IndexOf(e3, anchoring.Rankings[vote])-1)+"3");
		
	for(int e=0; e<toCheck.size(); e++)
	{
		toCheckf.add(e, toCheck.get(e).substring(1));
	}
	
	if(this.SinglePlateau(toCheckf)) //check the single peakedness of the the created ranking with respect to the canonical ordering 1,2,3
	{
		return true;
	}else return false;
}

//------------------------------checks if a profile of rankings is single peaked with respect to the given order "ref"---------------------------------------------------------

public boolean singlePeakedProfile(int[] votes, int ref)
{
	int length=votes.length;
	int counter=0;
	for(int i=0; i<length; i++)
	{
		if(GsinglePeaked(votes[i], ref))
			counter=counter+1;
	}	
	if(counter ==length) {return true;}else {return false;} // the profile is single peaked if 75% of the rankings therein are single peaked
}

//------------------------------checks if a profile of rankings is weakly single peaked with respect to the given order "ref"---------------------------------------------------------

public boolean WsinglePeakedProfile(int[] votes, int ref)
{
	int length=votes.length;
	int counter=0;
	for(int i=0; i<length; i++)
	{
		if(GWsinglePeaked(votes[i], ref))
			counter=counter+1;
	}
	
	if(counter ==length) {return true;}else {return false;} // the profile is single peaked if 75% of the rankings therein are single peaked
}

//------------------------------checks if a profile of rankings is single plateau with respect to the given order "ref"---------------------------------------------------------

public boolean SinglePlateauProfile(int[] votes, int ref)
{
	int length=votes.length;
	int counter=0;
	for(int i=0; i<length; i++)
	{
		if(GSinglePlateau(votes[i], ref))
			counter=counter+1;
	}
	if(counter ==length) 
	{
		return true;
	}else {return false;} // the profile is single peaked if 75% of the rankings therein are single peaked
}

//------------------------------checks if a profile of rankings is weakly single peaked with respect to any order "ref"---------------------------------------------------------

public boolean fullWSinglePeaked(int[] votes)
{
	boolean check=false;
	for(int ref=0; ref< 13; ref++)
	{
		if( !check && anchoring.Rankings[ref].size()== 3)
		{
			if(this.WsinglePeakedProfile(votes, ref))
			{
				check=true;  //to stop the loop if an ordering is already found with respect to which the votes are single peaked
			}
		}
	}

	return check;
}

//------------------------------checks if a profile of rankings is weakly single peaked with respect to any order "ref"---------------------------------------------------------

public boolean fullSinglePeaked(int[] votes)
{
	boolean check=false;
	for(int ref=0; ref< 13; ref++)
	{
		if( !check && anchoring.Rankings[ref].size()== 3)
		{
			if(this.singlePeakedProfile(votes, ref))
			{
				check=true;  //to stop the loop if an ordering is already found with respect to which the votes are single peaked
			}
		}
	}

	return check;
}

//------------------------------------------checks if an ordering is  strict------------------------------------


public static boolean strict(List<String> vote)
	{
	if(vote.size()< 3)
	{
		return false;
	}else
	{
		return true;
	}
}
//------------------------------------------checks for strict profiles------------------------------------


public boolean strictprof(int[] vote, int q)
{
	int counter1=0;
	for(int u=0; u< q; u++)
	{
		List<String> z=anchoring.Rankings[vote[u]];
		if(strict(z))
		{
			counter1=counter1+1;
		}
	}
	if (counter1 >= (0.74)*q)
	{
		return true;
	}else return false;
}

//---------------------------------------------proximity to weak single peakedness---------------


public double proxWSP(int[] vote)
{
	int groupSize= vote.length;
	int[] SP=new int[6];
	for(int a=0; a<6;a++)
	{
		SP[a]=0;
	}
	for(int j=0; j< 6; j++)
	{
		for(int i=0; i<groupSize; i++)
		{
			if(GWsinglePeaked(vote[i], j+7))
			{
				SP[j]=SP[j]+1;
			}
		}
	}
	int max=0;
	for(int g=0; g<6; g++)
	{
		if(SP[g]> max)
		{
			max=SP[g];
		}
	}
	return ((double) max)/groupSize;
}


//--------------------------------------------proximity to single plateaudness----------------


public double proxSPl(int[] vote)
{
	int groupSize= vote.length;
	int[] SP=new int[6];
	for(int a=0; a<6;a++)
	{
		SP[a]=0;
	}

	for(int j=0; j< 6; j++)
	{
		for(int i=0; i<groupSize; i++)
		{
			if(GSinglePlateau(vote[i], j+7))
			{
				SP[j]=SP[j]+1;
			}
		}
	}

	int max=0;
	for(int g=0; g<6; g++)
	{
		if(SP[g]> max)
		{
			max=SP[g];
		}
	}
	return ((double) max)/groupSize;
}

//-----------------------------------------proximity to single peakedness-------------------


public double proxSPeak(int[] vote)
{
	int groupSize= vote.length;
	int[] SP=new int[6];
	for(int a=0; a<6;a++)
	{
	 SP[a]=0;
	}	
	for(int j=0; j< 6; j++)
	{	
		for(int i=0; i<groupSize; i++)
		{
			if(GsinglePeaked(vote[i], j+7))
			{
				SP[j]=SP[j]+1;
			}
		}
	}
	int max=0;
	for(int g=0; g<6; g++)
	{
		if(SP[g]> max)
		{
			max=SP[g];
		}
	}
	return ((double) max)/groupSize;
}

//-----------------------------------------Aggregating the votes after deliberation-------------------


public int aggregate(int[] vote)
{

	int Result=5000;
	boolean[][] PairwiseMajorityMatrix=new boolean[3][3];
	PairwiseMajorityMatrix= pairwiseWcomparison(vote);
	if(checkforNtran(vote))
	{	
		Result=1000;
	}else
	{
		List<String> ranking= new ArrayList<String>(); 
		String top = new String("");
		String middle = new String("");
		String last = new String("");
		int[] SumOfRows=new int[3];
		for(int k=0; k<3; k++)
		{
			SumOfRows[k]=0;
		}
		for(int r=0; r<3;r++)
		{
			for(int j=0; j<3;j++)
			{
				if(PairwiseMajorityMatrix[r][j])
				{
				SumOfRows[r]=SumOfRows[r]+1;
				}
			}
		}
		
		for(int t=0; t<3;t++)
		{
			if(SumOfRows[t] ==3)
			{
				top=top+IntegerToString(t+1);
			}
			if(SumOfRows[t] ==2)
			{
				middle=middle+IntegerToString(t+1);
			}
			if(SumOfRows[t] ==1)
			{
				last=last+IntegerToString(t+1);
			}
		}
		
		if(!top.isEmpty())
		{
			ranking.add(top);
		}
	
		if(!middle.isEmpty())
		{
			ranking.add(middle);
		}
		if(!last.isEmpty())
		{
			ranking.add(last);
		}
		boolean check=false;
		for(int p=0; p<13;p++)
		{
			if(ranking.equals(anchoring.Rankings[p]))
			{
				Result=p;
				check=true;

			}
		}
	
		if(!check)
		{
			Result=2000;
		}
	}
return Result;
}





//-----------------------------------------------------------------------------------------------------------------------------

public static void main(String[] a)
{

	//-------------Reading the config file-------------------------------------------------------------------------------
	String configFilePath = "config.txt"; // Assuming the config file is in the same directory as the code
    int minGroupSize = 3;
    int maxGroupSize = 119;
    int groupSizeIncrement = 4;
    String localPathForGeneratedDataSets = "/home/soroush/Desktop/anchoring-sim-2/";

    try {
        BufferedReader configReader = new BufferedReader(new FileReader(configFilePath));
        String line;
        while ((line = configReader.readLine()) != null) {
            String[] parts = line.split("=");
            if (parts.length == 2) {
                String key = parts[0].trim();
                String value = parts[1].trim();
                switch (key) {
                    case "minimum group size":
                        minGroupSize = Integer.parseInt(value);
                        break;
                    case "maximum group size":
                        maxGroupSize = Integer.parseInt(value);
                        break;
                    case "group size increment":
                        groupSizeIncrement = Integer.parseInt(value);
                        break;
                    case "local path for generated data sets":
                        localPathForGeneratedDataSets = value;
                        break;
                }
            }
        }
        configReader.close();
    } catch (Exception e) {
        System.out.println("Error reading config file: " + e.getMessage());
    }

	
	
	//-------------Preparing the output file-------------------------------------------------------------------------------

	String outputfile2= localPathForGeneratedDataSets+"anchoring.csv"; //Local path for generated data sets-read from config file
	try
	{
		FileWriter fw = new FileWriter(outputfile2,true);
		BufferedWriter bw = new BufferedWriter(fw);
		PrintWriter pw2 = new PrintWriter(bw);
			pw2.println("Number of Agents"+","+"Average first speaker Anchoring DP"+","+"Average expert anchoring DP"+","+"Diff. expert & first speaker anch. DP"+","+"Average first speaker Anchoring KS"+","+"Average expert anchoring KS"+","+"Diff. expert & first speaker anch. KS"+","+"Average first speaker Anchoring CS"+","+"Average expert anchoring CS"+","+"Diff. expert & first speaker anch. CS"+","+"Avr. Ini. Prox Sin.Peak"+","+"Avr. DP. Prox Sin.Peak"+","+"Avr. KS Prox Sin.Peak"+","+"Avr CS Prox Sin.Peak"+","+"Avr Ini Prox Sin.Pl"+","+"Avr DP Prox Sin.Pl"+","+"Avr KS Prox Sin.Pl"+","+"Avr CS Prox Sin.Pl"+","+"DP aver. dist2Consensus"+","+"KS aver. dist2Consensus"+","+"CS aver. dist2Consensus"+","+ "ini Anc. DP"+","+ "ini Anc. KS"+","+ "ini Anc. CS"+","+"Expert ini Anc. DP"+","+ "Expert ini Anc. KS"+","+ "Expert ini Anc. CS"+","+ "num ini anchored DP"+","+ "ini double anchored DP"+","+ "num ini anchored KS"+","+ "ini double anchored KS"+","+ "num ini anchored CS"+","+ "ini double anchored CS");
		
		pw2.flush();
		pw2.close();
	}
	catch(Exception E)
	{
		JOptionPane.showMessageDialog(null,"Data NOT Saved10");
	}
	
	
	for(int q=minGroupSize; q< maxGroupSize+1; q += groupSizeIncrement)   //running the code for different group sizes
	{
				
			int numAg=q;	
			
				
				
			//------------------------preparing the output file----------------------------------
			
			String nuAg=String.valueOf(numAg);
			String outputfile= localPathForGeneratedDataSets +"GroupSize: "+ q+".csv"; //Local path for generated data sets - read from the config file
			try
			{
				FileWriter fw = new FileWriter(outputfile,true);
				BufferedWriter bw = new BufferedWriter(fw);
				PrintWriter pw = new PrintWriter(bw);
				for(int k=0; k<numAg; k++)
				{
					pw.print("reliability"+ k +","+"Agent "+ k+ " ini"+","+"Agent "+ k+ " DP"+","+"Agent "+ k+ " KS"+","+"Agent "+ k+ " CS"+ "," +"distance to Agent "+ k+" DP"+ "," +"distance to Agent "+ k+" KS"+ "," +"distance to Agent "+ k+" CS"+",");
				}
				
				pw.println("ini Transitive"+","+ "DP transitive" +","+ "KS Transitive"+","+ "CS transitive"+","+"expert"+ ","+"Distance to the expert DP"+","+"distance to expert KS"+","+"Distance to the expert CS"+","+"NumVotes0"+","+"NumVotes1"+","+"NumVotes2"+","+"NumVotes3"+","+"NumVotes4"+","+"NumVotes5"+","+"NumVotes6"+","+"NumVotes7"+","+"NumVotes8"+","+"NumVotes9"+","+"NumVotes10"+","+"NumVotes11"+","+"NumVotes12"+","+"freq vote Ag1"+","+"Aver Expertise in group"+","+"Diff expertise- First speaker and group average"+","+"Diff expertise-first speaker and expert"+","+"Diff expertise- expert and group average"+","+"ini prox to SPeak"+","+"ini prox Spl"+","+"DP prox to SPeak"+","+"DP prox Spl"+ ","+"KS prox to SPeak"+","+"KS prox Spl"+","+"CS prox to SPeak"+","+"CS prox Spl"+","+"Self-expert");
								//pw.println("expert"+ ","+"Distance to the expert DP"+","+"distance to expert KS"+","+"Distance to the expert CS"+","+"NumVotes0"+","+"NumVotes1"+","+"NumVotes2"+","+"NumVotes3"+","+"NumVotes4"+","+"NumVotes5"+","+"freq vote Ag1"+","+"Aver Expertise in group"+","+"Diff expertise- First speaker and group average"+","+"Diff expertise-first speaker and expert"+","+"Diff expertise- expert and group average"+","+"ini prox to SPeak"+","+"ini prox Spl"+"DP prox to SPeak"+","+"DP prox Spl"+ ","+"KS prox to SPeak"+","+"KS prox Spl"+","+"CS prox to SPeak"+","+"CS prox Spl"); //for strict case
				pw.flush();
				pw.close();
			}
			catch (Exception E)
			{
				JOptionPane.showMessageDialog(null,"Data NOT Saved 11");
			}
			
			//-------------------------------------------------------------------------------------------
			
			
			
			int[] DelibResultsKS;
			int[] DelibResultsDP;
			int[] DelibResultsCS;
			int[] votes;
			int DPconsensuscounter=0;
			int KSconsensuscounter=0;
			int CSconsensuscounter=0;
			int wsinglePeakedCounter=0;
			int singlePeakedCounter=0;
			int singlePlateauedCounter=0;
			int DPsinglePeakedResultCounter=0;
			int KSsinglePeakedResultCounter=0;
			int CSsinglePeakedResultCounter=0;
			int DPwsinglePeakedResultCounter=0;
			int KSwsinglePeakedResultCounter=0;
			int CSwsinglePeakedResultCounter=0;
			
			int iniDPFirAnchoringcounter=0;
			int iniKSFirAnchoringcounter=0;
			int iniCSFirAnchoringcounter=0;
			int iniDPExpAnchoringcounter=0;
			int iniKSExpAnchoringcounter=0;
			int iniCSExpAnchoringcounter=0;
			int DPFirAnchoringcounter=0;
			int KSFirAnchoringcounter=0;
			int CSFirAnchoringcounter=0;
			int DPExpAnchoringcounter=0;
			int KSExpAnchoringcounter=0;
			int CSExpAnchoringcounter=0;
			double DPSinglePlateau=0;
			double KSSinglePlateau=0;
			double CSSinglePlateau=0;
			double iniProximitytoWSP=0;
			double KSProximitytoWSP=0;
			double DPProximitytoWSP=0;
			double CSProximitytoWSP=0;
			double iniProximitytoSPl=0;
			double KSProximitytoSPl=0;
			double DPProximitytoSPl=0;
			double CSProximitytoSPl=0;
			double iniProximitytoSPeak=0;
			double KSProximitytoSPeak=0;
			double DPProximitytoSPeak=0;
			double CSProximitytoSPeak=0;	
			
			double AvrIniProxSPl=0;
			double AvrDPProxSPl=0;
			double AvrKSProxSPl=0;
			double AvrCSProxSPl=0;
			double AvrIniProxSP=0;
			double AvrDPProxSP=0;
			double AvrKSProxSP=0;
			double AvrCSProxSP=0;
			
			
			double[] DPinitdistances2Csqr= new double[13];
			double[] KSinitdistances2Csqr= new double[13];
			double[] CSinitdistances2Csqr= new double[13];		
			double[] DPinitdistances2C= new double[13];
			double[] KSinitdistances2C= new double[13];
			double[] CSinitdistances2C= new double[13];	
			double CSinitdist2C=0;
			double KSinitdist2C=0;
			double DPinitdist2C=0;
			double[] DPdistances2Csqr= new double[13];
			double[] KSdistances2Csqr= new double[13];
			double[] CSdistances2Csqr= new double[13];
			double[] DPdistances2C= new double[13];
			double[] KSdistances2C= new double[13];
			double[] CSdistances2C= new double[13];			
			double[] iniDPdistances2iniVsqr= new double[numAg];
			double[] iniKSdistances2iniVsqr= new double[numAg];
			double[] iniCSdistances2iniVsqr= new double[numAg];			
			double[] DPdistances2iniVsqr= new double[numAg];
			double[] KSdistances2iniVsqr= new double[numAg];
			double[] CSdistances2iniVsqr= new double[numAg];
			double[] iniDPdistances2iniV= new double[numAg];
			double[] iniKSdistances2iniV= new double[numAg];
			double[] iniCSdistances2iniV= new double[numAg];	
			double[] DPdistances2iniV= new double[numAg];
			double[] KSdistances2iniV= new double[numAg];
			double[] CSdistances2iniV= new double[numAg];
			
			double CSresultdist2C=0;
			double KSresultdist2C=0;
			double DPresultdist2C=0;
			
			double CSdiffdist2C=0;
			double KSdiffdist2C=0;
			double DPdiffdist2C=0;
			
			double CSmeandiffdist2C=0;
			double KSmeandiffdist2C=0;
			double DPmeandiffdist2C=0;
			
			double KSdiffAnch=0;
			double DPdiffAnch=0;
			double CSdiffAnch=0;
			
			double CSmeandiffAnch=0;
			double KSmeandiffAnch=0;
			double DPmeandiffAnch=0;
			
			
			double	AvrDPFirAnch=0;
			double	AvrKSFirAnch=0;
			double	AvrCSFirAnch=0;
			double	AvrDPExpAnch=0;
			double	AvrKSExpAnch=0;
			double	AvrCSExpAnch=0;	
			double cyc=0;      //counter for initial cycles
			double DPcyc=0; //counter for cycles after DP deliberation
			double KScyc=0;   //counter for cycles after KS deliberation
			double CScyc=0;  //counter for cycles after CS deliberation
			int initNonTransitive=0;
			int DPNtran=0;
			int KSNtran=0;
			int CSNtran=0;
			int DistinguishedExpert=200;
			
			int iniNotanchoredDP=0;
			int iniNotanchoredKS=0;
			int iniNotanchoredCS=0;
			
			int inianchoredcounterDP=0;
			int inianchoredcounterKS=0;
			int inianchoredcounterCS=0;

			int inidoubleanchoredDP=0;
			int inidoubleanchoredKS=0;
			int inidoubleanchoredCS=0;
			//-------------------------------------------------------------------------------------------------------------------------------------------------------
			int numSimulations=1000;
			for(int w=0; w<numSimulations;w++)
			{
				double[] Rel=new double[numAg];
				
				//Assigning Reliabilities Randomly--------------------------------------------------------
				Random rand=new Random();
				
				for(int i=0; i< numAg;i++)
				{
				Rel[i]=(rand.nextDouble());  //setting reliabilities  randomly
				}
				//-----------------------------------------------------------

				//for assigning the reliabilities to agents with significant difference between the expert and the rest
				//-----------------------------------------------------------
				
//				Random rand2=new Random();
//				DistinguishedExpert= rand2.nextInt(numAg);
//				for(int i=0; i< numAg;i++)
//				{
//						Rel[i]=0;  //initialising
//					
//				}
//				Rel[DistinguishedExpert]= (rand.nextDouble()*(0.2))+0.3;
//
//				for(int i=0; i< numAg;i++)
//				{
//					if(i != DistinguishedExpert)
//					{
//						Rel[i]=(rand.nextDouble()*(0.2));  //setting reliabilities  randomly
//				
//					}
//				}
//				
						
				anchoring p= new anchoring(numAg,20, Rel);		//initializing the group			
				boolean selfexpert = true;
				
				for(int i=0; i< numAg;i++) //checking if self-reliability is less than 0.5 for some agent in a given run
				{
				if(Rel[i] > 0.5)
					selfexpert = false;  
				
				}
				
					boolean check0=true;
					boolean DPcheck=true;
					boolean KScheck=true;
					boolean CScheck=true;
					boolean wcheck0=true;
					boolean DPwcheck=true;
					boolean KSwcheck=true;
					boolean CSwcheck=true;
					boolean pcheck0=true;
					boolean DPpcheck=true;
					boolean KSpcheck=true;
					boolean CSpcheck=true;
					double sumExpertise=0;
					double AveExpertise= 0;
					double iniProximSPeak=0;
					double iniProximSPl=0;
					double DPProximSPeak=0;
					double DPProximSPl=0;
					double KSProximSPeak=0;
					double KSProximSPl=0;
					double CSProximSPeak=0;
					double CSProximSPl=0;
					double FirSpea_ExpDiff_GroupAve=0; //difference between expertise of first speaker and group average 
					double Expert_ExpDiff_GroupAve=0; //difference between expertise of group expert and group average 
					double FirSpea_ExpDiff_Expert=0; //difference between expertise of first speaker and group average 
					boolean IsTransitiveINI=true;
					boolean IsTransitiveDP=true;
					boolean IsTransitiveKS=true;
					boolean IsTransitiveCS=true;
					int expert=100;
					expert= p.expert();
					
					int[] VotesFrequency= new int[13];//non strict voting
					for(int i=0; i<13; i++)
					{
						VotesFrequency[i]=0;
					}
					
					//int[] VotesFrequency= new int[6]; //strict voting
					//for(int i=0; i<6; i++)
					//{
					//	VotesFrequency[i]=0;
					//}
					
					for(int i=0; i< numAg;i++)
					{
					sumExpertise= sumExpertise+ Rel[i];  //sum of reliabilities
					}
					AveExpertise= ((double)sumExpertise)/numAg;
					FirSpea_ExpDiff_GroupAve=Rel[0]-AveExpertise;
					Expert_ExpDiff_GroupAve=Rel[expert]-AveExpertise;
					FirSpea_ExpDiff_Expert=Rel[0]-Rel[expert];
					
					//votes=p.votingS();          //random voting first speaker separated
					votes=p.voting();            //random voting
					//votes=p.Stvoting();		//random voting with strict rankings
	
					
					for(int j=0; j<numAg; j++)
					{
						int temp=0;
						temp=votes[j];
					VotesFrequency[temp]=VotesFrequency[temp]+1;	
					}
	
				
					DelibResultsDP= p.DelibDP(votes);			//deliberation with DP
					DelibResultsKS= p.DelibKS(votes);			//deliberation with KS
					DelibResultsCS= p.DelibCS(votes);			//deliberation with CS
					//DelibResultsDP= p.DelibDPRandom(votes);		//DP Deliberation with random order
					//DelibResultsKS= p.DelibKSRandom(votes);		//KS Deliberation with random order
					//DelibResultsCS= p.DelibCSRandom(votes);		//CS Deliberation with random order
					//DelibResultsDP= p.DelibStDPRandom(votes);  	//for limiting to strict rankings-random order
					//DelibResultsKS= p.DelibStKSRandom(votes);		//for limiting to strict rankings-random order
					//DelibResultsCS= p.DelibStCSRandom(votes);		//for limiting to strict rankings-random order	
					//DelibResultsKS=p.DelibStKS(votes);	
					//DelibResultsDP=p.DelibStDP(votes);	
					//DelibResultsCS=p.DelibStCS(votes);			
					
	
		//-------------------------------------------------------------------------------------checking for cycles in initial votes
					if( p.checkforCycles(votes))
					{
						cyc=cyc+1;
					}
					
					
		//-------------------------------------------------------------------------------------checking for Non transitive initial votes
					if( p.checkforNtran(votes))
					{
						initNonTransitive=initNonTransitive+1;
						IsTransitiveINI=false;

					}
					
					
		//-------------------------------------------------------------------------------------checking for cycles and intransitivity in deliberation results
					if( p.checkforCycles(DelibResultsDP))
					{
						DPcyc=DPcyc+1;
					}
					if( p.checkforNtran(DelibResultsDP))
					{
						DPNtran=DPNtran+1;
						IsTransitiveDP=false;
	
					}
					if( p.checkforCycles(DelibResultsKS))
					{
						KScyc=KScyc+1;
					}
					if( p.checkforNtran(DelibResultsKS))
					{
						KSNtran=KSNtran+1;
						IsTransitiveKS=false;
					}
					if( p.checkforCycles(DelibResultsCS))
					{
						CScyc=CScyc+1;
					}
					if( p.checkforNtran(DelibResultsCS))
					{
						CSNtran=CSNtran+1;
						IsTransitiveCS=false;
	
					}
					
			
					
		//***************************(weak)Single-peaked & Single-plateued Calculations*****************************			
		//**********************************************************************************************************			
		//**********************************************************************************************************			
		//**********************************************************************************************************					
		//--------checking if initial votes are weakly single-peaked with respect to any ordering--------------------
					
					for(int ref=0; ref< 13; ref++)
					{
						if( wcheck0 && anchoring.Rankings[ref].size()== 3)
						{
							if(p.WsinglePeakedProfile(votes, ref))
							{
								wsinglePeakedCounter= wsinglePeakedCounter+1; //adding 1 to the single peaked counter
								wcheck0=false;  //to stop the loop if an ordering is already found with respect to which the votes are single peaked
							}
						}
					}
					
					
		//--------checking if initial votes are single-peaked with respect to any ordering-------------------------
					
					for(int ref=0; ref< 13; ref++)
					{
						if( check0 &&anchoring.Rankings[ref].size()== 3)//limiting to those that do not put two alternatives at the same rank and thus give an ordering of three alternatives
						{
							if(p.singlePeakedProfile(votes, ref))
							{
								singlePeakedCounter= singlePeakedCounter+1; //adding 1 to the single peaked counter if the votes are single peaked
								check0=false;  //to stop the loop if an ordering is already found with respect to which the votes are single peaked
		
							}
						}
					}
								
	
		//--------checking if initial votes are single-plateaued with respect to any ordering----------------------
					
					for(int ref=0; ref< 13; ref++)
					{
						if( pcheck0 &&anchoring.Rankings[ref].size()== 3)//limiting to those that do not put two alternatives at the same rank and thus give an ordering of three alternatives
						{
							if(p.SinglePlateauProfile(votes, ref))
							{
								singlePlateauedCounter= singlePlateauedCounter+1; //adding 1 to the single peaked counter if the votes are single peaked
								pcheck0=false;  //to stop the loop if an ordering is already found with respect to which the votes are single peaked
							}
						}
					}
					
		//**********************Proximity to (weak)Single-peaked & Single-plateued Calculations*********************			
		//**********************************************************************************************************			
		//**********************************************************************************************************			
		//**********************************************************************************************************							
		//------------------------------------calculating Proximity for each simulation:----------------------------
				
					iniProximSPeak=p.proxSPeak(votes);
					iniProximSPl=p.proxSPl(votes);	
					DPProximSPeak=p.proxSPeak(DelibResultsDP);
					DPProximSPl=p.proxSPl(DelibResultsDP);
					KSProximSPeak=p.proxSPeak(DelibResultsKS);
					KSProximSPl=p.proxSPl(DelibResultsKS);
					CSProximSPeak=p.proxSPeak(DelibResultsCS);
					CSProximSPl=p.proxSPl(DelibResultsCS);
					
		//-------------calculating proximities over all simulations for averaging-------------------------------	
		//-------------to weak single-peakedness-------------------------------
					
					iniProximitytoWSP= iniProximitytoWSP+ p.proxWSP(votes); //to weak Single peakedness initial votes
					KSProximitytoWSP= KSProximitytoWSP+ p.proxWSP(DelibResultsKS); //to weak Single-peakedness-after deliberation with KS
					DPProximitytoWSP= DPProximitytoWSP+ p.proxWSP(DelibResultsDP); //to weak Single-peakedness-after deliberation with DP
					CSProximitytoWSP= CSProximitytoWSP+ p.proxWSP(DelibResultsCS);  //to weak Single-peakedness-after deliberation with CS
						
		//-------------to single-peakedness-------------------------------------
			
					iniProximitytoSPeak=iniProximitytoSPeak+p.proxSPeak(votes); //to strict Single peakedness
					KSProximitytoSPeak= KSProximitytoSPeak+ p.proxSPeak(DelibResultsKS); //to Single-peakedness-after deliberation with KS
					DPProximitytoSPeak= DPProximitytoSPeak+ p.proxSPeak(DelibResultsDP); //to Single-peakedness-after deliberation with DP
					CSProximitytoSPeak= CSProximitytoSPeak+ p.proxSPeak(DelibResultsCS); //to Single-peakedness-after deliberation with CS
						
		//------------to single-pleatuedness---------------------------------------		
						
					iniProximitytoSPl= iniProximitytoSPl+ p.proxSPl(votes); //to Single-plateauedness
					KSProximitytoSPl= KSProximitytoSPl+ p.proxSPl(DelibResultsKS); //to Single-plateauedness-after deliberation with KS
					DPProximitytoSPl= DPProximitytoSPl+ p.proxSPl(DelibResultsDP); //to Single-plateauedness-after deliberation with DP
					CSProximitytoSPl= CSProximitytoSPl+ p.proxSPl(DelibResultsCS);	//to Single pelateauedness-after deliberation with CS
						
						
		//**********************Anchoring Calculations***********************************************			
		//*******************************************************************************************			
		//*******************************************************************************************		
		//*******************************************************************************************												
					//-----aggregating the result of deliberation
					//-------------------------------------------------------------------------------
					//-----initializing the variables
					int DPaggregated=25;
					int KSaggregated=25;
					int CSaggregated=25;
					int iniaggregated=25;
					//------calculate the aggregated value--------
					
					iniaggregated=p.aggregate(votes);
					DPaggregated= p.aggregate(DelibResultsDP);
					KSaggregated= p.aggregate(DelibResultsKS);				
					CSaggregated= p.aggregate(DelibResultsCS);
					//-------checking if the aggregation has not worked out
					if(iniaggregated==5000) System.out.println("error ini not aggregated");
					if(DPaggregated==5000) System.out.println("error DP not aggregated");
					if(KSaggregated==5000) System.out.println("error KS not aggregated");
					if(CSaggregated==5000) System.out.println("error CS not aggregated");
					//------check if he aggregated value is incorrectly mapped to a ranking
					if(iniaggregated==2000) System.out.println("error-ini mistekenly aggregated");
					if(DPaggregated==2000) System.out.println("error-DP mistekenly aggregated");
					if(KSaggregated==2000) System.out.println("error-KS mistekenly aggregated");
					if(CSaggregated==2000) System.out.println("error-CS mistekenly aggregated");
					//-----if the deliberation result is transitive and aggregation has worked out correctly
					//-----calculating the distance of the aggregated value to the initial votes---------
					if(iniaggregated <13)
					{
						for(int l=0; l< numAg; l++)
						{
							iniDPdistances2iniV[l]= DPDistanceMatrix[iniaggregated][votes[l]];
						}
					}
					if(iniaggregated <13)
					{
						for(int l=0; l< numAg; l++)
						{
							iniKSdistances2iniV[l]= KDistanceMatrix[iniaggregated][votes[l]];
						}
					}
					if(iniaggregated <13)
					{
						for(int l=0; l< numAg; l++)
						{
							iniCSdistances2iniV[l]= CSDistanceMatrix[iniaggregated][votes[l]];
						}
					}
					
					if(DPaggregated <13)
					{
						for(int l=0; l< numAg; l++)
						{
							DPdistances2iniV[l]= DPDistanceMatrix[DPaggregated][votes[l]];
						}
					}
					if(KSaggregated <13)
					{
						for(int l=0; l< numAg; l++)
						{
							KSdistances2iniV[l]= KDistanceMatrix[KSaggregated][votes[l]];
						}
					}
					if(CSaggregated <13)
					{
						for(int l=0; l< numAg; l++)
						{
							CSdistances2iniV[l]= CSDistanceMatrix[CSaggregated][votes[l]];
						}
					}
					//-------checking if the aggregation of initial vote gives intransitive result------- 				
					if(iniaggregated == 1000)
					{
					//------calculating the distances between deliberation result profile and each of the initial rankings--------------
					//--------------initializing variables--------------
						
						for(int l=0; l< numAg; l++)
						{
						iniDPdistances2iniVsqr[l]=0;    
						iniKSdistances2iniVsqr[l]=0;    
						iniCSdistances2iniVsqr[l]=0;    
						}
						
			    	//--------------calculating the distances--------------
						
						for(int l=0; l< numAg; l++) 
						{
							for(int r=0; r< numAg; r++)
							{
								iniDPdistances2iniVsqr[l]= iniDPdistances2iniVsqr[l]+Math.pow(anchoring.DPDistanceMatrix[votes[r]][votes[l]],2);
							}
							for(int r=0; r< numAg; r++)
							{
								iniKSdistances2iniVsqr[l]= iniKSdistances2iniVsqr[l]+Math.pow(anchoring.KDistanceMatrix[votes[r]][votes[l]],2);
							}
							for(int r=0; r< numAg; r++)
							{
								iniCSdistances2iniVsqr[l]= iniCSdistances2iniVsqr[l]+Math.pow(anchoring.CSDistanceMatrix[votes[r]][votes[l]],2);
							}
						}
						
						for(int l=0; l< numAg; l++)
						{
							iniDPdistances2iniV[l]= Math.sqrt(iniDPdistances2iniVsqr[l]);
							iniKSdistances2iniV[l]= Math.sqrt(iniKSdistances2iniVsqr[l]);
							iniCSdistances2iniV[l]= Math.sqrt(iniCSdistances2iniVsqr[l]);
	
						}
						
					}
					
					//-------checking if the deliberation result gives intransitive aggregation------- 				
					if(DPaggregated == 1000)
					{
					//------calculating the distances between deliberation result profile and each of the initial rankings--------------
					//--------------initializing variables--------------
						
						for(int l=0; l< numAg; l++)
						{
						DPdistances2iniVsqr[l]=0;    
						}
						
			    	//--------------calculating the distances--------------
						
						for(int l=0; l< numAg; l++) 
						{
							for(int r=0; r< numAg; r++)
							{
								DPdistances2iniVsqr[l]= DPdistances2iniVsqr[l]+Math.pow(anchoring.DPDistanceMatrix[DelibResultsDP[r]][votes[l]],2);
							}
						}
						
						for(int l=0; l< numAg; l++)
						{
							DPdistances2iniV[l]= Math.sqrt(DPdistances2iniVsqr[l]);
			
						}
						
					}
							
					if(KSaggregated == 1000)
					{
					//------calculating the distances between deliberation result profile and each of the initial rankings--------------
					//--------------initializing variables--------------
						
						for(int l=0; l< numAg; l++)
						{
						KSdistances2iniVsqr[l]=0;    
						}
					//--------------calculating the distances--------------
						
						for(int l=0; l< numAg; l++) 
						{
							for(int r=0; r< numAg; r++)
							{
								KSdistances2iniVsqr[l]= KSdistances2iniVsqr[l]+Math.pow(anchoring.KDistanceMatrix[DelibResultsKS[r]][votes[l]],2);
							}
						}
						for(int l=0; l< numAg; l++)
						{
							KSdistances2iniV[l]= Math.sqrt(KSdistances2iniVsqr[l]);
			
						}
						
					}
						
					if(CSaggregated == 1000)
					{
					//------calculating the distances between deliberation result profile and each of the initial rankings--------------
					//--------------initializing variables-------------- 		
						for(int l=0; l< numAg; l++)
						{
						CSdistances2iniVsqr[l]=0;
						}
						
				    //--------------calculating the distances--------------
						
						for(int l=0; l< numAg; l++) 
						{
							for(int r=0; r< numAg; r++)
							{
								CSdistances2iniVsqr[l]= CSdistances2iniVsqr[l]+Math.pow(anchoring.CSDistanceMatrix[DelibResultsCS[r]][votes[l]],2);
							}
						}
						
						for(int l=0; l< numAg; l++)
						{
						    CSdistances2iniV[l]= Math.sqrt(CSdistances2iniVsqr[l]);
						}
					}
						
					//-----------writing the distances to the output file-------------
						
						try
						{
							FileWriter fw = new FileWriter(outputfile,true);
							BufferedWriter bw = new BufferedWriter(fw);
							PrintWriter pw = new PrintWriter(bw);
								
							for(int i=0; i<numAg; i++)
							{				
							pw.print(Rel[i]+","+votes[i]+","+DelibResultsDP[i]+","+DelibResultsKS[i]+","+DelibResultsCS[i]+","+DPdistances2iniV[i]+","+KSdistances2iniV[i]+","+CSdistances2iniV[i]+",");
							}
							pw.println(IsTransitiveINI+","+IsTransitiveDP+","+IsTransitiveKS+","+ IsTransitiveCS+","+expert+","+DPdistances2iniV[expert]+","+KSdistances2iniV[expert]+","+CSdistances2iniV[expert]+","+VotesFrequency[0]+","+VotesFrequency[1]+","+VotesFrequency[2]+","+VotesFrequency[3]+","+VotesFrequency[4]+","+VotesFrequency[5]+","+VotesFrequency[6]+","+VotesFrequency[7]+","+VotesFrequency[8]+","+VotesFrequency[9]+","+VotesFrequency[10]+","+VotesFrequency[11]+","+VotesFrequency[12]+","+VotesFrequency[votes[0]]+","+AveExpertise+","+FirSpea_ExpDiff_GroupAve+","+FirSpea_ExpDiff_Expert+","+Expert_ExpDiff_GroupAve+","+iniProximSPeak+","+iniProximSPl+","+DPProximSPeak+","+DPProximSPl+","+KSProximSPeak+","+KSProximSPl+","+CSProximSPeak+","+CSProximSPl+","+selfexpert);
							pw.flush();
							pw.close();
							//JOptionPane.showMessageDialog(null,"Data Saved");
						}
						catch(Exception E)
						{
							JOptionPane.showMessageDialog(null,"Data NOT Saved 12");
						}
									
			
					////----------------check for anchoring by first speaker--------------------------------
						
						//System.out.println(p.checkiniAnchoring(iniDPdistances2iniV, votes));
						if(p.checkiniAnchoring(iniDPdistances2iniV))
						{
							iniNotanchoredDP=iniNotanchoredDP+1;
						}
						if(p.checkiniAnchoring(iniKSdistances2iniV))
						{
							iniNotanchoredKS=iniNotanchoredKS+1;
						}
						if(p.checkiniAnchoring(iniCSdistances2iniV))
						{
							iniNotanchoredCS=iniNotanchoredCS+1;
						}
						
						inidoubleanchoredDP=inidoubleanchoredDP+p.checkiniAnchoring2(iniDPdistances2iniV,votes);
						inidoubleanchoredKS=inidoubleanchoredKS+p.checkiniAnchoring2(iniKSdistances2iniV,votes);
						inidoubleanchoredCS=inidoubleanchoredCS+p.checkiniAnchoring2(iniCSdistances2iniV,votes);
	
						
						if(p.checkAnchoring(iniDPdistances2iniV))
						{
							iniDPFirAnchoringcounter=iniDPFirAnchoringcounter+1;
						}
						if(p.checkAnchoring(iniKSdistances2iniV))
						{
							iniKSFirAnchoringcounter=iniKSFirAnchoringcounter+1;
						}
						if(p.checkAnchoring(iniCSdistances2iniV))
						{
							iniCSFirAnchoringcounter=iniCSFirAnchoringcounter+1;
						}
							
						if(p.checkAnchoringEx(iniDPdistances2iniV, expert))
						{
							iniDPExpAnchoringcounter=iniDPExpAnchoringcounter+1;
						}
						if(p.checkAnchoringEx(iniKSdistances2iniV, expert))
						{
							iniKSExpAnchoringcounter=iniKSExpAnchoringcounter+1;
						}
						if(p.checkAnchoringEx(iniCSdistances2iniV, expert))
						{
							iniCSExpAnchoringcounter=iniCSExpAnchoringcounter+1;
						}

						if(p.checkAnchoring(DPdistances2iniV))
						{
							DPFirAnchoringcounter=DPFirAnchoringcounter+1;
						}
						
						if(p.checkAnchoring(KSdistances2iniV))
						{
							KSFirAnchoringcounter=KSFirAnchoringcounter+1;
						}
						
						if(p.checkAnchoring(CSdistances2iniV))
						{
						CSFirAnchoringcounter=CSFirAnchoringcounter+1;
						}
					////----------------check for anchoring by expert--------------------------------
						
						
						if(p.checkAnchoringEx(DPdistances2iniV, expert))
						{
							DPExpAnchoringcounter=DPExpAnchoringcounter+1;
						}
						
						if(p.checkAnchoringEx(KSdistances2iniV, expert))
						{
							KSExpAnchoringcounter=KSExpAnchoringcounter+1;
						}
						
						if(p.checkAnchoringEx(CSdistances2iniV, expert))
						{
						CSExpAnchoringcounter=CSExpAnchoringcounter+1;
						}
						
					////----------------Difference between anchoring by the expert and by the first speaker--------------------------------
		
						
						KSdiffAnch=DPExpAnchoringcounter-DPFirAnchoringcounter;
						DPdiffAnch=KSExpAnchoringcounter-KSFirAnchoringcounter;
						CSdiffAnch=CSExpAnchoringcounter-CSFirAnchoringcounter;
	
						
						
		//--------------------------calculating the distances to consensus------------------------------------------------------------------
			  ////--------------------initializing variables-------------------------------------			
						for(int l=0; l<13; l++)
					{
						DPdistances2Csqr[l]=0;    //initializing the array distances2sqr to zero
						KSdistances2Csqr[l]=0;
						CSdistances2Csqr[l]=0;
						DPinitdistances2Csqr[l]=0;    //initializing the array initdistances2sqr to zero
						KSinitdistances2Csqr[l]=0;
						CSinitdistances2Csqr[l]=0;
					}
		    ////-----------------Calculating distance of the initial vote to consensus----------------------------------------------------
					for(int t=0; t< 13; t++)
					{
						for(int l=0; l<numAg; l++)
						{
							DPinitdistances2Csqr[t]= DPinitdistances2Csqr[t]+Math.pow(anchoring.DPDistanceMatrix[t][votes[l]],2); //sum of the squares of the distances between each possible ranking t (i.e. any potential consensus) to the votes in the profile according to DP
						}
						for(int l=0; l<numAg; l++)
						{
							KSinitdistances2Csqr[t]= KSinitdistances2Csqr[t]+Math.pow(anchoring.KDistanceMatrix[t][votes[l]],2); //sum of the squares of the distances between each possible ranking t (i.e. any potential consensus) to the votes in the profile according to KS
						}
						for(int l=0; l<numAg; l++)
						{
							CSinitdistances2Csqr[t]= CSinitdistances2Csqr[t]+Math.pow(anchoring.CSDistanceMatrix[t][votes[l]],2); //sum of the squares of the distances between each possible ranking t (i.e. any potential consensus) to the votes in the profile according to CS
						}
					}
					for(int t=0; t< 13; t++)
					{
						DPinitdistances2C[t]= Math.sqrt(DPinitdistances2Csqr[t]); //The distances between each possible ranking t (i.e. any potential consensus) to the vote profile according to DP
						KSinitdistances2C[t]= Math.sqrt(KSinitdistances2Csqr[t]); //The distances between each possible ranking t (i.e. any potential consensus) to the vote profile according to KS
						CSinitdistances2C[t]= Math.sqrt(CSinitdistances2Csqr[t]); //The distances between each possible ranking t (i.e. any potential consensus) to the vote profile according to CS
					}
		   ////-------------------Calculating distance to consensus after deliberation----------------------------------------------------
					for(int t=0; t< 13; t++)
					{
						for(int l=0; l<numAg; l++)
						{
							DPdistances2Csqr[t]= DPdistances2Csqr[t]+Math.pow(anchoring.DPDistanceMatrix[t][DelibResultsDP[l]],2);
						}
						for(int l=0; l<numAg; l++)
						{
							KSdistances2Csqr[t]= KSdistances2Csqr[t]+Math.pow(anchoring.KDistanceMatrix[t][DelibResultsKS[l]],2);
						}
						for(int l=0; l<numAg; l++)
						{
							CSdistances2Csqr[t]= CSdistances2Csqr[t]+Math.pow(anchoring.CSDistanceMatrix[t][DelibResultsCS[l]],2);
						}
					}
					for(int t=0; t< 13; t++)
					{
						DPdistances2C[t]= Math.sqrt(DPdistances2Csqr[t]);
						KSdistances2C[t]= Math.sqrt(KSdistances2Csqr[t]);
						CSdistances2C[t]= Math.sqrt(CSdistances2Csqr[t]);
					}
					
	//--------------finding the ranking with the minimal average distance to after-deliberation votes--------------
					
					int counterDPinit= 0;
					for(int t=1; t<13; t++)
					{
						if(DPinitdistances2C[t]< DPinitdistances2C[counterDPinit])
						{
							counterDPinit= t;					// The ranking with minimum distance to initial votes according to DP (i.e. DP best choice for the consensus)
							DPinitdist2C= DPinitdistances2C[t]; // The minimum distance of initial votes to a consensus according to DP
						}
					}
					int counterDPRes= 0;
					for(int t=1; t<13; t++)
					{
						if(DPdistances2C[t]< DPdistances2C[counterDPRes]) 	
						{
							counterDPRes= t; 				   // The ranking with minimum distance to votes after DP deliberation, according to DP (i.e. DP best choice for the consensus)
							DPresultdist2C= DPdistances2C[t]; // The minimum distance of votes after DP deliberation to a consensus according to DP
						}
					}
					DPdiffdist2C= DPdiffdist2C+(DPinitdist2C - DPresultdist2C); //move towards consensus by DP deliberation
					
					int counterKSinit= 0;
					for(int t=1; t<13; t++)
					{
						if(KSinitdistances2C[t]< KSinitdistances2C[counterKSinit])
						{
							counterKSinit= t;                      // The ranking with minimum distance to initial votes according to KS (i.e. KS best choice for the consensus)			
								KSinitdist2C= KSinitdistances2C[t];// The minimum distance of initial votes to a consensus according to KS			
						}
					}
					int counterKSres= 0;
					for(int t=1; t<13; t++)
					{
						if(KSdistances2C[t]< KSdistances2C[counterKSres])
						{
							counterKSres= t; // The ranking with minimum distance to votes after KS deliberation, according to KS (i.e. DP best choice for the consensus)
							KSresultdist2C= KSdistances2C[t]; // The minimum distance of votes after KS deliberation to a consensus according to KS
						}
					}
					KSdiffdist2C= KSdiffdist2C+(KSinitdist2C - KSresultdist2C); //move towards consensus by KS deliberation
					
					
					int counterCSinit= 0;
					for(int t=1; t<13; t++)
					{
						if(CSinitdistances2C[t]< CSinitdistances2C[counterCSinit])
						{
							counterCSinit= t; // The ranking with minimum distance to initial votes according to CS (i.e. CS best choice for the consensus)					
							CSinitdist2C= CSinitdistances2C[t];// The minimum distance of initial votes to a consensus according to CS		
						}
					}
					int counterCSres= 0;
					for(int t=1; t<13; t++)
					{
						if(CSdistances2C[t]< CSdistances2C[counterCSres])
						{
							counterCSres= t; // The ranking with minimum distance to votes after DP deliberation, according to CS (i.e. DP best choice for the consensus)
							CSresultdist2C= CSdistances2C[t]; // The minimum distance of votes after CS deliberation to a consensus according to CS
							
						}
					}
					CSdiffdist2C= CSdiffdist2C+(CSinitdist2C - CSresultdist2C); //move towards consensus by CS deliberation
					
					
					
		//------------check for consensus-----------------------------------------------------
					if(p.checkconsensus(DelibResultsDP))
					{
						DPconsensuscounter=DPconsensuscounter+1;
					}
					if(p.checkconsensus(DelibResultsKS))
					{
						KSconsensuscounter=KSconsensuscounter+1;
					}
					if(p.checkconsensus(DelibResultsCS))
					{
						CSconsensuscounter=CSconsensuscounter+1;
					}
		
					
		//--------------------check for weak single peakedness--------------------------------------------
					for(int ref=0; ref< 13; ref++)
					{
				////---------------DP deliberation----------------------------
						if(DPwcheck && anchoring.Rankings[ref].size()== 3)
						{
							if(p.WsinglePeakedProfile(DelibResultsDP, ref))
							{
								DPwsinglePeakedResultCounter= DPwsinglePeakedResultCounter+1;
								DPwcheck=false;
							}
						}
			////-------------------- KS deliberation------------------------
						if(KSwcheck && anchoring.Rankings[ref].size()== 3)
						{
							if(p.WsinglePeakedProfile(DelibResultsKS, ref))
							{
								KSwsinglePeakedResultCounter= KSwsinglePeakedResultCounter+1;
								KSwcheck=false;
							}
						}
			////-------------------- CS deliberation------------------------
						if(CSwcheck && anchoring.Rankings[ref].size()== 3)
						{
							if(p.WsinglePeakedProfile(DelibResultsCS, ref))
							{
								CSwsinglePeakedResultCounter= CSwsinglePeakedResultCounter+1;	
								CSwcheck=false;
							}
						}
					}	
					
					
		//---------check for single-peakedness--------------------------------------------
		
					for(int ref=0; ref< 13; ref++)
					{
						////--------------DP deliberation DP-----------
						if(DPcheck && anchoring.Rankings[ref].size()== 3)
						{
							if(p.singlePeakedProfile(DelibResultsDP, ref) || p.checkconsensus(DelibResultsDP))
							{
								DPsinglePeakedResultCounter= DPsinglePeakedResultCounter+1;	
								DPcheck=false;
							}
						}
						////---------------KS deliberation----------------
						if(KScheck && anchoring.Rankings[ref].size()== 3)
						{
							if(p.singlePeakedProfile(DelibResultsKS, ref) || p.checkconsensus(DelibResultsKS))
							{
								KSsinglePeakedResultCounter= KSsinglePeakedResultCounter+1;
								KScheck=false;
							}
						}
						////---------------CS deliberation--------------------------------
						if(CScheck && anchoring.Rankings[ref].size()== 3)
						{
							if(p.singlePeakedProfile(DelibResultsCS, ref) || p.checkconsensus(DelibResultsCS))
							{
								CSsinglePeakedResultCounter= CSsinglePeakedResultCounter+1;	
								CScheck=false;
							}
						}
					}
		
		//---------------check for single-plateauedness--------------------------------------------
					for(int ref=0; ref< 13; ref++)
					{
						////---------------DP deliberation--------------------------------
						if(DPpcheck && anchoring.Rankings[ref].size()== 3)
						{
							if(p.SinglePlateauProfile(DelibResultsDP, ref))
							{
								DPSinglePlateau= DPSinglePlateau+1;															
								DPpcheck=false;
							}
						}
						////---------------KS deliberation--------------------------------
						if(KSpcheck && anchoring.Rankings[ref].size()== 3)
						{
							if(p.SinglePlateauProfile(DelibResultsKS, ref))
							{
								KSSinglePlateau= KSSinglePlateau+1;	
								KSpcheck=false;
							}
						}
						////---------------CS deliberation--------------------------------
						if(CSpcheck && anchoring.Rankings[ref].size()== 3)
						{
							if(p.SinglePlateauProfile(DelibResultsCS, ref))
							{
								CSSinglePlateau= CSSinglePlateau+1;
								CSpcheck=false;
							}
						}
					}
	
				}
			
	//----------------calculating the averages and printing the results--------------
			inianchoredcounterDP=numSimulations-iniNotanchoredDP;
			inianchoredcounterKS=numSimulations-iniNotanchoredKS;
			inianchoredcounterCS=numSimulations-iniNotanchoredCS;
			CSmeandiffdist2C= (CSdiffdist2C/numSimulations);
			KSmeandiffdist2C= (KSdiffdist2C/numSimulations);
			DPmeandiffdist2C= (DPdiffdist2C/numSimulations);
			DPmeandiffAnch= (DPdiffAnch/numSimulations);
			CSmeandiffAnch= (CSdiffAnch/numSimulations);
			KSmeandiffAnch= (KSdiffAnch/numSimulations);
			
			AvrIniProxSPl=((double)iniProximitytoSPl)/numSimulations; //average proximity to single-plateauness initial votes
			AvrDPProxSPl=((double)DPProximitytoSPl)/numSimulations;   //average proximity to single-plateauness after DP deliberation
			AvrKSProxSPl=((double)KSProximitytoSPl)/numSimulations;  //average proximity to single-plateauness after KS deliberation			  
			AvrCSProxSPl=((double)CSProximitytoSPl)/numSimulations;   //average proximity to single-plateauness after CS deliberation

			AvrIniProxSP=((double)iniProximitytoSPeak)/numSimulations; //average proximity to single-peakedness initial votes
			AvrDPProxSP=((double)DPProximitytoSPeak)/numSimulations; //average proximity to single-peakedness after DP deliberation
			AvrKSProxSP=((double)KSProximitytoSPeak)/numSimulations; //average proximity to single-peakedness after KS deliberation
			AvrCSProxSP=((double)CSProximitytoSPeak)/numSimulations;	//average proximity to single-peakedness after CS deliberation		
				
			
			double iniAvrDPFirAnch = ((double)iniDPFirAnchoringcounter)/numSimulations; //average anchoring by first speaker initial votes DP-perspective 			
			double iniAvrKSFirAnch = ((double)iniKSFirAnchoringcounter)/numSimulations; //average anchoring by first speaker initial votes KS-perspective			
			double iniAvrCSFirAnch = ((double)iniCSFirAnchoringcounter)/numSimulations; //average anchoring by first speaker initial votes CS-perspective			

			
			double AvriniDPExpAnch=((double)iniDPExpAnchoringcounter)/numSimulations; //average anchoring by expert initial votes DP-perspective 			
			double AvriniKSExpAnch=((double)iniKSExpAnchoringcounter)/numSimulations; //average anchoring by expert initial votes KS-perspective			
			double AvriniCSExpAnch=((double)iniCSExpAnchoringcounter)/numSimulations; //average anchoring by expert initial votes CS-perspective			

			
			
			AvrDPFirAnch=((double)DPFirAnchoringcounter)/numSimulations; //average anchoring by first speaker DP deliberation			
			AvrKSFirAnch=((double)KSFirAnchoringcounter)/numSimulations; //average anchoring by first speaker KS deliberation				
			AvrCSFirAnch=((double)CSFirAnchoringcounter)/numSimulations; //average anchoring by first speaker CS deliberation			
				
			AvrDPExpAnch=((double)DPExpAnchoringcounter)/numSimulations; //average anchoring by expert DP deliberation			
			AvrKSExpAnch=((double)KSExpAnchoringcounter)/numSimulations; //average anchoring by expert KS deliberation				
			AvrCSExpAnch=((double)CSExpAnchoringcounter)/numSimulations; //average anchoring by expert CS deliberation			
			
			try
			{
				FileWriter fw = new FileWriter(outputfile2,true);
				BufferedWriter bw = new BufferedWriter(fw);
				PrintWriter pw2 = new PrintWriter(bw);
					pw2.println(numAg+","+AvrDPFirAnch+","+AvrDPExpAnch+","+DPmeandiffAnch+","+AvrKSFirAnch+","+AvrKSExpAnch+","+KSmeandiffAnch+","+AvrCSFirAnch+","+AvrCSExpAnch+","+CSmeandiffAnch+","+AvrIniProxSP+","+AvrDPProxSP+","+AvrKSProxSP+","+AvrCSProxSP+","+AvrIniProxSPl+","+AvrDPProxSPl+","+AvrKSProxSPl+","+AvrCSProxSPl+","+DPmeandiffdist2C+","+KSmeandiffdist2C+","+CSmeandiffdist2C+","+ iniAvrDPFirAnch +","+ iniAvrKSFirAnch+","+ iniAvrCSFirAnch+","+AvriniDPExpAnch +","+AvriniKSExpAnch+","+ AvriniCSExpAnch+","+inianchoredcounterDP+","+inidoubleanchoredDP+","+inianchoredcounterKS+","+inidoubleanchoredKS+","+inianchoredcounterCS+","+inidoubleanchoredCS);
				pw2.flush();
				pw2.close();
				//JOptionPane.showMessageDialog(null,"Data Saved");
			}
			catch(Exception E)
			{
				JOptionPane.showMessageDialog(null,"Data NOT Saved 13");
			}
			
	System.out.println("------------"+q+"--------------------");
	System.out.println("--------------"+q+"------------------");
	System.out.println("----------------"+q+"----------------");
	System.out.println("------------------"+q+"--------------");
	}
}	
}
